Code for Figures 1 and S1; Table S2 and S3

Load libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggrepel) #for repelling labels 
library(corrplot) #plotting correlations 
## corrplot 0.94 loaded
library(vegan) #for permanova 
## Loading required package: permute
## Loading required package: lattice
library(ggfortify) #plotting PCAs
library(viridis) #for color scale
## Loading required package: viridisLite
#making topo maps:
library(elevatr)
## elevatr v0.99.0 NOTE: Version 0.99.0 of 'elevatr' uses 'sf' and 'terra'.  Use 
## of the 'sp', 'raster', and underlying 'rgdal' packages by 'elevatr' is being 
## deprecated; however, get_elev_raster continues to return a RasterLayer.  This 
## will be dropped in future versions, so please plan accordingly.
library(terra)
## terra 1.7.78
## 
## Attaching package: 'terra'
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(giscoR)
library(marmap)
## Registered S3 methods overwritten by 'adehabitatMA':
##   method                       from
##   print.SpatialPixelsDataFrame sp  
##   print.SpatialPixels          sp  
## 
## Attaching package: 'marmap'
## 
## The following object is masked from 'package:terra':
## 
##     as.raster
## 
## The following object is masked from 'package:grDevices':
## 
##     as.raster

Load Climate Data

all_clim <- read_csv("../Processed.Data/All_Clim.csv")
## Rows: 2951 Columns: 23
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): parent.pop, elevation.group, timeframe, Season
## dbl (19): elev_m, Lat, Long, year, cwd, pck, ppt, tmn, tmx, ann_tmean, mean_...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Map of populations (Figure 1a)

pops_garden_locs <- all_clim %>% 
  mutate_at(c("Lat", "Long"), as.double) %>% 
  select(parent.pop:Long) %>%
  filter(!is.na(Lat), !is.na(Long)) %>% 
  distinct()
states <- map_data("state") %>% filter(region == "california")

ggplot() +
  geom_polygon(data = states, aes(x = long, y = lat, group = group), fill = "gray") +
  coord_quickmap(xlim = c(-125, -114), ylim = c(35.8, 41))+
  geom_point(data = pops_garden_locs,
             aes(x = Long, y = Lat, color=elev_m),
             size = 3) +
  geom_label_repel(data = pops_garden_locs,
         aes(x = Long, y = Lat,
             label = `parent.pop`),
         min.segment.length = 0,
         max.overlaps = 100,
         #force = 3,
         box.padding = 0.4,
         label.padding = 0.15,
         label.size = 0.1,
         size = 3) +
  labs(color="Elevation (m)") +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  theme_void()

#ggsave("../Figures/Pop_Map_Figure1a.png", width = 10, height = 10, units = "in")

Climate variable correlations (Figure S1)

Calculate 30 year climate averages

all_clim_avgs <- all_clim %>% 
  select(-Lat, -Long) %>% 
  filter(parent.pop!="WL2_Garden") %>% #remove the garden 
  left_join(pops_garden_locs) %>% #add in Lat Long for growth season rows 
  group_by(parent.pop, elevation.group, elev_m, Lat, Long, 
           timeframe, Season) %>% 
  summarise_at(c("cwd",  "pck", "ppt", "tmn", "tmx",
                 "ann_tmean", "mean_diurnal_range", "temp_seasonality", "temp_ann_range",
                 "tmean_wettest_month", "tmean_driest_month", "ann_ppt",
                 "ppt_seasonality","ppt_warmest_month", "ppt_coldest_month"), 
               c(mean), na.rm = TRUE)
## Joining with `by = join_by(parent.pop, elevation.group, elev_m)`

Recent Corrs - Water Year

recent_wtr_yr_avgs_normalized <- all_clim_avgs %>% 
  filter(timeframe=="recent", Season=="Water Year") %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_recent_wtr_yr = cor(recent_wtr_yr_avgs_normalized) #test correlations among the traits
cor.sig_recent_wtr_yr <- cor.mtest(recent_wtr_yr_avgs_normalized, method = "pearson") #get pearson's test p-values 

corrplot(cor.norm_recent_wtr_yr, type="upper",
         tl.srt = 45, p.mat = cor.sig_recent_wtr_yr$p, 
         sig.level = 0.05, insig="blank")

#800 x 734

Historical Corrs - Water year

historic_wtr_yr_avgs_normalized <- all_clim_avgs %>% 
  filter(timeframe=="historic", Season=="Water Year") %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_historic_wtr_yr = cor(historic_wtr_yr_avgs_normalized) #test correlations among the traits
cor.sig_historic_wtr_yr <- cor.mtest(historic_wtr_yr_avgs_normalized, method = "pearson") #get pearson's test p-values 

corrplot(cor.norm_historic_wtr_yr, type="upper",
         tl.srt = 45, p.mat = cor.sig_historic_wtr_yr$p, 
         sig.level = 0.05, insig="blank")

#800 x 734

Recent Corrs - Growth Season

recent_grwssn_avgs_normalized <- all_clim_avgs %>% 
  filter(timeframe=="recent", Season=="Growth Season") %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_recent_grwssn = cor(recent_grwssn_avgs_normalized) #test correlations among the traits
cor.sig_recent_grwssn <- cor.mtest(recent_grwssn_avgs_normalized, method = "pearson") #get pearson's test p-values 

corrplot(cor.norm_recent_grwssn, type="upper",
         tl.srt = 45, p.mat = cor.sig_recent_grwssn$p, 
         sig.level = 0.05, insig="blank")

#800 x 734

Historical Corrs - Growth Season

historic_grwssn_avgs_normalized <- all_clim_avgs %>% 
  filter(timeframe=="historic", Season=="Growth Season") %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_historic_grwssn = cor(historic_grwssn_avgs_normalized) #test correlations among the traits
cor.sig_historic_grwssn <- cor.mtest(historic_grwssn_avgs_normalized, method = "pearson") #get pearson's test p-values 

corrplot(cor.norm_historic_grwssn, type="upper",
         tl.srt = 45, p.mat = cor.sig_historic_grwssn$p, 
         sig.level = 0.05, insig="blank")

#800 x 734

Climate Change PCAs (Figure 1b-c; Table S2, S3)

Water Year + Growth Season PCA

all_clim_avgs_normalized <- all_clim_avgs %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_all_clim = cor(all_clim_avgs_normalized) #test correlations among the traits
cor.sig_all_clim <- cor.mtest(all_clim_avgs_normalized, method = "pearson") #get pearson's test p-values 
cor.norm_all_clim
##                             cwd        pck         ppt        tmn         tmx
## cwd                  1.00000000 -0.2287498 -0.68573712 -0.0134324  0.04595927
## pck                 -0.22874977  1.0000000  0.61626461 -0.7591419 -0.77353131
## ppt                 -0.68573712  0.6162646  1.00000000 -0.3027205 -0.34226623
## tmn                 -0.01343240 -0.7591419 -0.30272051  1.0000000  0.96260675
## tmx                  0.04595927 -0.7735313 -0.34226623  0.9626068  1.00000000
## ann_tmean            0.01706911 -0.7737570 -0.32598345  0.9901929  0.99101334
## mean_diurnal_range   0.21708743 -0.1723083 -0.19348476  0.0190141  0.28915677
## temp_seasonality     0.11349854  0.1077625 -0.11055240  0.1560643  0.13878469
## temp_ann_range      -0.07017480  0.2200251  0.07792712  0.1678923  0.24197343
## tmean_wettest_month -0.11069433 -0.7061974 -0.17994504  0.9150856  0.93011678
## tmean_driest_month  -0.19435550 -0.5442037 -0.05490368  0.9163906  0.90533049
## ann_ppt             -0.58681288  0.6751946  0.83242618 -0.2030886 -0.26652143
## ppt_seasonality      0.45320966 -0.4098026 -0.50007608  0.3669448  0.33117772
## ppt_warmest_month    0.08057386  0.4016879  0.16324816 -0.7907549 -0.72164104
## ppt_coldest_month   -0.41599094  0.4470107  0.70632506 -0.2575033 -0.34958982
##                       ann_tmean mean_diurnal_range temp_seasonality
## cwd                  0.01706911         0.21708743       0.11349854
## pck                 -0.77375698        -0.17230835       0.10776246
## ppt                 -0.32598345        -0.19348476      -0.11055240
## tmn                  0.99019290         0.01901410       0.15606430
## tmx                  0.99101334         0.28915677       0.13878469
## ann_tmean            1.00000000         0.15850928       0.14863206
## mean_diurnal_range   0.15850928         1.00000000      -0.03926838
## temp_seasonality     0.14863206        -0.03926838       1.00000000
## temp_ann_range       0.20768768         0.29977367       0.73240942
## tmean_wettest_month  0.93150950         0.19916343      -0.10543534
## tmean_driest_month   0.91937136         0.10307335       0.26023006
## ann_ppt             -0.23772612        -0.26600057       0.31273084
## ppt_seasonality      0.35197689        -0.07438720       0.42048538
## ppt_warmest_month   -0.76260617         0.13091279      -0.45026480
## ppt_coldest_month   -0.30743347        -0.38029689       0.24003812
##                     temp_ann_range tmean_wettest_month tmean_driest_month
## cwd                   -0.070174796         -0.11069433        -0.19435550
## pck                    0.220025089         -0.70619743        -0.54420374
## ppt                    0.077927122         -0.17994504        -0.05490368
## tmn                    0.167892266          0.91508558         0.91639057
## tmx                    0.241973427          0.93011678         0.90533049
## ann_tmean              0.207687685          0.93150950         0.91937136
## mean_diurnal_range     0.299773672          0.19916343         0.10307335
## temp_seasonality       0.732409418         -0.10543534         0.26023006
## temp_ann_range         1.000000000          0.14122358         0.44829436
## tmean_wettest_month    0.141223584          1.00000000         0.90171111
## tmean_driest_month     0.448294362          0.90171111         1.00000000
## ann_ppt                0.509871275         -0.16579372         0.11074853
## ppt_seasonality       -0.074098202          0.04380177         0.11867806
## ppt_warmest_month     -0.531790582         -0.67767175        -0.85748973
## ppt_coldest_month      0.003805134         -0.40209905        -0.15636269
##                        ann_ppt ppt_seasonality ppt_warmest_month
## cwd                 -0.5868129      0.45320966        0.08057386
## pck                  0.6751946     -0.40980256        0.40168790
## ppt                  0.8324262     -0.50007608        0.16324816
## tmn                 -0.2030886      0.36694477       -0.79075486
## tmx                 -0.2665214      0.33117772       -0.72164104
## ann_tmean           -0.2377261      0.35197689       -0.76260617
## mean_diurnal_range  -0.2660006     -0.07438720        0.13091279
## temp_seasonality     0.3127308      0.42048538       -0.45026480
## temp_ann_range       0.5098713     -0.07409820       -0.53179058
## tmean_wettest_month -0.1657937      0.04380177       -0.67767175
## tmean_driest_month   0.1107485      0.11867806       -0.85748973
## ann_ppt              1.0000000     -0.43501416       -0.17013396
## ppt_seasonality     -0.4350142      1.00000000       -0.26799276
## ppt_warmest_month   -0.1701340     -0.26799276        1.00000000
## ppt_coldest_month    0.6224590      0.09610056        0.07931787
##                     ppt_coldest_month
## cwd                      -0.415990941
## pck                       0.447010712
## ppt                       0.706325057
## tmn                      -0.257503272
## tmx                      -0.349589817
## ann_tmean                -0.307433466
## mean_diurnal_range       -0.380296894
## temp_seasonality          0.240038118
## temp_ann_range            0.003805134
## tmean_wettest_month      -0.402099052
## tmean_driest_month       -0.156362688
## ann_ppt                   0.622458989
## ppt_seasonality           0.096100558
## ppt_warmest_month         0.079317867
## ppt_coldest_month         1.000000000
cor.sig_all_clim$p
##                              cwd          pck          ppt          tmn
## cwd                 0.000000e+00 2.828863e-02 4.634672e-14 8.988745e-01
## pck                 2.828863e-02 0.000000e+00 6.179294e-11 1.810007e-18
## ppt                 4.634672e-14 6.179294e-11 0.000000e+00 3.356580e-03
## tmn                 8.988745e-01 1.810007e-18 3.356580e-03 0.000000e+00
## tmx                 6.635417e-01 1.603748e-19 8.393525e-04 7.815943e-53
## ann_tmean           8.717040e-01 1.541760e-19 1.518799e-03 9.940488e-79
## mean_diurnal_range  3.765251e-02 1.004997e-01 6.460663e-02 8.572314e-01
## temp_seasonality    2.813728e-01 3.065585e-01 2.941308e-01 1.373934e-01
## temp_ann_range      5.062359e-01 3.507850e-02 4.602988e-01 1.096595e-01
## tmean_wettest_month 2.935075e-01 3.748704e-15 8.608624e-02 2.909867e-37
## tmean_driest_month  6.338963e-02 2.058996e-08 6.031962e-01 1.492460e-37
## ann_ppt             7.884992e-10 1.565663e-13 8.446334e-25 5.218450e-02
## ppt_seasonality     5.719256e-06 4.978961e-05 3.865235e-07 3.208603e-04
## ppt_warmest_month   4.451574e-01 7.227049e-05 1.199788e-01 6.900873e-21
## ppt_coldest_month   3.723067e-05 7.935131e-06 3.687875e-15 1.321005e-02
##                              tmx    ann_tmean mean_diurnal_range
## cwd                 6.635417e-01 8.717040e-01       0.0376525082
## pck                 1.603748e-19 1.541760e-19       0.1004996931
## ppt                 8.393525e-04 1.518799e-03       0.0646066342
## tmn                 7.815943e-53 9.940488e-79       0.8572314362
## tmx                 0.000000e+00 1.984739e-80       0.0051810497
## ann_tmean           1.984739e-80 0.000000e+00       0.1312633898
## mean_diurnal_range  5.181050e-03 1.312634e-01       0.0000000000
## temp_seasonality    1.870454e-01 1.573608e-01       0.7101588975
## temp_ann_range      2.013397e-02 4.697216e-02       0.0036949529
## tmean_wettest_month 6.344321e-41 2.645031e-41       0.0570008682
## tmean_driest_month  3.118116e-35 3.114846e-38       0.3282055725
## ann_ppt             1.022691e-02 2.249941e-02       0.0103815882
## ppt_seasonality     1.261386e-03 5.802552e-04       0.4809852222
## ppt_warmest_month   4.849532e-16 1.025864e-18       0.2135496230
## ppt_coldest_month   6.360701e-04 2.872789e-03       0.0001845122
##                     temp_seasonality temp_ann_range tmean_wettest_month
## cwd                     2.813728e-01   5.062359e-01        2.935075e-01
## pck                     3.065585e-01   3.507850e-02        3.748704e-15
## ppt                     2.941308e-01   4.602988e-01        8.608624e-02
## tmn                     1.373934e-01   1.096595e-01        2.909867e-37
## tmx                     1.870454e-01   2.013397e-02        6.344321e-41
## ann_tmean               1.573608e-01   4.697216e-02        2.645031e-41
## mean_diurnal_range      7.101589e-01   3.694953e-03        5.700087e-02
## temp_seasonality        0.000000e+00   1.072333e-16        3.171826e-01
## temp_ann_range          1.072333e-16   0.000000e+00        1.793403e-01
## tmean_wettest_month     3.171826e-01   1.793403e-01        0.000000e+00
## tmean_driest_month      1.223703e-02   7.418782e-06        1.554783e-34
## ann_ppt                 2.404473e-03   2.088611e-07        1.142336e-01
## ppt_seasonality         3.003691e-05   4.826958e-01        6.784446e-01
## ppt_warmest_month       6.687229e-06   4.902243e-08        1.181515e-13
## ppt_coldest_month       2.118393e-02   9.712834e-01        7.093519e-05
##                     tmean_driest_month      ann_ppt ppt_seasonality
## cwd                       6.338963e-02 7.884992e-10    5.719256e-06
## pck                       2.058996e-08 1.565663e-13    4.978961e-05
## ppt                       6.031962e-01 8.446334e-25    3.865235e-07
## tmn                       1.492460e-37 5.218450e-02    3.208603e-04
## tmx                       3.118116e-35 1.022691e-02    1.261386e-03
## ann_tmean                 3.114846e-38 2.249941e-02    5.802552e-04
## mean_diurnal_range        3.282056e-01 1.038159e-02    4.809852e-01
## temp_seasonality          1.223703e-02 2.404473e-03    3.003691e-05
## temp_ann_range            7.418782e-06 2.088611e-07    4.826958e-01
## tmean_wettest_month       1.554783e-34 1.142336e-01    6.784446e-01
## tmean_driest_month        0.000000e+00 2.932698e-01    2.598510e-01
## ann_ppt                   2.932698e-01 0.000000e+00    1.468956e-05
## ppt_seasonality           2.598510e-01 1.468956e-05    0.000000e+00
## ppt_warmest_month         1.031491e-27 1.049319e-01    9.800815e-03
## ppt_coldest_month         1.366338e-01 3.495685e-11    3.621517e-01
##                     ppt_warmest_month ppt_coldest_month
## cwd                      4.451574e-01      3.723067e-05
## pck                      7.227049e-05      7.935131e-06
## ppt                      1.199788e-01      3.687875e-15
## tmn                      6.900873e-21      1.321005e-02
## tmx                      4.849532e-16      6.360701e-04
## ann_tmean                1.025864e-18      2.872789e-03
## mean_diurnal_range       2.135496e-01      1.845122e-04
## temp_seasonality         6.687229e-06      2.118393e-02
## temp_ann_range           4.902243e-08      9.712834e-01
## tmean_wettest_month      1.181515e-13      7.093519e-05
## tmean_driest_month       1.031491e-27      1.366338e-01
## ann_ppt                  1.049319e-01      3.495685e-11
## ppt_seasonality          9.800815e-03      3.621517e-01
## ppt_warmest_month        0.000000e+00      4.523076e-01
## ppt_coldest_month        4.523076e-01      0.000000e+00
#tmn, tmx, and ann_tmean all highly correlated (~99%) - only keep ann_tmean 
#tmean_wettest_month, tmean_driest_month correlated to ann_tmean (92-93%) #low enough that it's okay to keep in?
all_clim_avgs <- all_clim_avgs %>% 
  ungroup()
all_clim_avgs.pc = prcomp(all_clim_avgs[c(8:10, 13:22)], scale = TRUE, center = TRUE)
summary(all_clim_avgs.pc)
## Importance of components:
##                           PC1    PC2    PC3    PC4     PC5     PC6     PC7
## Standard deviation     2.1774 1.8365 1.4004 1.2170 0.84233 0.59561 0.40598
## Proportion of Variance 0.3647 0.2594 0.1509 0.1139 0.05458 0.02729 0.01268
## Cumulative Proportion  0.3647 0.6241 0.7750 0.8889 0.94350 0.97079 0.98346
##                            PC8     PC9    PC10    PC11    PC12    PC13
## Standard deviation     0.34860 0.18939 0.16549 0.13308 0.08421 0.07339
## Proportion of Variance 0.00935 0.00276 0.00211 0.00136 0.00055 0.00041
## Cumulative Proportion  0.99281 0.99557 0.99768 0.99904 0.99959 1.00000
tibble(PC=str_c("PC",str_pad(1:13,2,pad="0")),
       percent_var=all_clim_avgs.pc$sdev[1:13]^2/sum(all_clim_avgs.pc$sdev^2)*100) %>%
  ggplot(aes(x=PC, y=percent_var)) +
  geom_col() +
  ggtitle("Percent Variance Explained")

#combine pcs with metadata
all_clim_avgs.pc.dat = data.frame(all_clim_avgs.pc$x)
all_clim_avgs_locs.pc = cbind(all_clim_avgs, all_clim_avgs.pc.dat) %>% 
  select(parent.pop, elev_m, , Lat, timeframe, Season, PC1:PC13)
all_clim_avgs_loadings = data.frame(varnames=rownames(all_clim_avgs.pc$rotation), all_clim_avgs.pc$rotation)
all_clim_avgs_loadings
##                                varnames         PC1         PC2         PC3
## cwd                                 cwd  0.14446177 -0.33401643 -0.36724535
## pck                                 pck -0.39672970  0.12437285 -0.14643867
## ppt                                 ppt -0.30176555  0.32488652  0.23622589
## ann_tmean                     ann_tmean  0.42036562  0.16559971  0.11279901
## mean_diurnal_range   mean_diurnal_range  0.12382589 -0.09054416  0.01370599
## temp_seasonality       temp_seasonality  0.06972744  0.25812989 -0.58680076
## temp_ann_range           temp_ann_range  0.07787411  0.36449214 -0.32785687
## tmean_wettest_month tmean_wettest_month  0.37858973  0.16923017  0.31924374
## tmean_driest_month   tmean_driest_month  0.34831759  0.33439808  0.12376799
## ann_ppt                         ann_ppt -0.23967311  0.45114929 -0.01428814
## ppt_seasonality         ppt_seasonality  0.20365767 -0.10653496 -0.42664790
## ppt_warmest_month     ppt_warmest_month -0.31109476 -0.33795903  0.09264125
## ppt_coldest_month     ppt_coldest_month -0.25952374  0.25048649 -0.12287029
##                              PC4          PC5          PC6          PC7
## cwd                 -0.102990058  0.101413149 -0.795837215  0.275447577
## pck                 -0.184780377  0.156323196 -0.229876271 -0.589365923
## ppt                  0.048315458 -0.271126907 -0.292783632  0.003290068
## ann_tmean            0.100966780 -0.126054397 -0.081091635 -0.037650424
## mean_diurnal_range  -0.625723835 -0.662118047  0.009749555 -0.099195595
## temp_seasonality    -0.028045353  0.003257024  0.307749857  0.339035467
## temp_ann_range      -0.444187620  0.076600109  0.074559483 -0.021651414
## tmean_wettest_month -0.013797935 -0.025500470 -0.184539325 -0.047328677
## tmean_driest_month   0.008909443 -0.024702448 -0.116071246  0.068377919
## ann_ppt             -0.056904564  0.081847195 -0.208249823  0.039931280
## ppt_seasonality      0.419608647 -0.360018679 -0.001947351 -0.544187999
## ppt_warmest_month   -0.112112933 -0.266912567  0.107921840  0.243882075
## ppt_coldest_month    0.401188818 -0.472001283 -0.136350921  0.293198514
##                             PC8         PC9        PC10        PC11
## cwd                 -0.01747894  0.02172680 -0.01129035 -0.03257132
## pck                 -0.21004848 -0.51208665 -0.15714441  0.10553798
## ppt                 -0.22467617  0.31207660 -0.23556293 -0.53899045
## ann_tmean           -0.26851038 -0.24549828  0.14018307  0.30883422
## mean_diurnal_range   0.19203256  0.02968044 -0.24196588  0.15756979
## temp_seasonality    -0.37424144 -0.02178072 -0.47105647  0.04507448
## temp_ann_range       0.12480600  0.02174131  0.60130425 -0.29498858
## tmean_wettest_month -0.22904438  0.04188202 -0.05954223  0.16793716
## tmean_driest_month  -0.14781689 -0.31314009  0.05305711 -0.32059329
## ann_ppt             -0.06111790  0.46797580  0.15437221  0.57794749
## ppt_seasonality     -0.13811108  0.30050397  0.16732303 -0.07152068
## ppt_warmest_month   -0.64563678 -0.08764400  0.42587050  0.03496942
## ppt_coldest_month    0.35569528 -0.40126015  0.13592167  0.12918603
##                              PC12        PC13
## cwd                 -0.0009643873 -0.02465757
## pck                  0.0464951928  0.05068626
## ppt                  0.1405206842 -0.26905115
## ann_tmean            0.0702565916 -0.70479456
## mean_diurnal_range  -0.1229475409  0.01289754
## temp_seasonality     0.0895164196  0.03534944
## temp_ann_range       0.2792624649 -0.04208390
## tmean_wettest_month  0.5641151147  0.53365674
## tmean_driest_month  -0.6494509014  0.29224726
## ann_ppt             -0.3130862856  0.07282531
## ppt_seasonality     -0.0728573362  0.12457321
## ppt_warmest_month   -0.0437448176  0.12827175
## ppt_coldest_month    0.1636497646  0.13053122
#PC1 pos ann_tmean loading
#PC2 pos for ann_ppt

Permanova

all_clim_avgs_locs.pc_dist <- all_clim_avgs_locs.pc %>% ungroup() %>% select(PC1:PC13)
dist_matrix_allclim <- dist(all_clim_avgs_locs.pc_dist, method = "euclidian") #use a distance function to calculate euclidian distance in PCA space
permanova_results_allclim <- adonis2(dist_matrix_allclim ~ Season*timeframe*elev_m*Lat, data = all_clim_avgs_locs.pc) #use adonis2 to run the permanova
permanova_results_allclim #look at output 
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = dist_matrix_allclim ~ Season * timeframe * elev_m * Lat, data = all_clim_avgs_locs.pc)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model    15   840.39 0.71039 12.428  0.001 ***
## Residual 76   342.61 0.28961                  
## Total    91  1183.00 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#get stats per term in the model:
permanova_results_allclim_terms <- adonis2(dist_matrix_allclim ~ Season*timeframe*elev_m*Lat, data = all_clim_avgs_locs.pc, by = "terms")
permanova_results_allclim_terms
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = dist_matrix_allclim ~ Season * timeframe * elev_m * Lat, data = all_clim_avgs_locs.pc, by = "terms")
##                             Df SumOfSqs      R2       F Pr(>F)    
## Season                       1   175.71 0.14853 38.9781  0.001 ***
## timeframe                    1    47.62 0.04026 10.5646  0.001 ***
## elev_m                       1   342.73 0.28972 76.0285  0.001 ***
## Lat                          1   102.02 0.08624 22.6315  0.001 ***
## Season:timeframe             1     3.35 0.00283  0.7420  0.541    
## Season:elev_m                1   100.19 0.08469 22.2260  0.001 ***
## timeframe:elev_m             1     9.19 0.00777  2.0390  0.078 .  
## Season:Lat                   1    15.28 0.01292  3.3899  0.008 ** 
## timeframe:Lat                1     3.25 0.00275  0.7210  0.567    
## elev_m:Lat                   1    21.23 0.01794  4.7087  0.003 ** 
## Season:timeframe:elev_m      1     4.16 0.00352  0.9233  0.434    
## Season:timeframe:Lat         1     2.11 0.00178  0.4671  0.781    
## Season:elev_m:Lat            1    11.65 0.00985  2.5850  0.041 *  
## timeframe:elev_m:Lat         1     0.59 0.00050  0.1315  0.990    
## Season:timeframe:elev_m:Lat  1     1.30 0.00110  0.2878  0.937    
## Residual                    76   342.61 0.28961                   
## Total                       91  1183.00 1.00000                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#LM on PCs (follow up on permanova)
lmer_results_allclim <- all_clim_avgs_locs.pc %>%
  ungroup() %>% 
  pivot_longer(starts_with("PC", ignore.case = FALSE), 
               names_to = "PC", values_to = "value") %>% 
  group_by(PC) %>% 
  nest(data=c(Season, timeframe, parent.pop, elev_m, Lat, value)) %>% 
  mutate(glm=map(data, ~ glm(value ~ Season*timeframe*elev_m*Lat,
                               data=.x)),
         anova = map(glm, ~ broom.mixed::tidy(anova(.x))))

PC_anova_all_clim <- lmer_results_allclim %>% select(-data, -glm) %>% unnest(anova) %>%
  select(PC, term, p.value) %>%
  filter(p.value < 0.05) %>%
  arrange(PC, p.value)
PC_anova_all_clim #PC1 and PC2 have significant timeframe effects 
## # A tibble: 58 × 3
## # Groups:   PC [13]
##    PC    term              p.value
##    <chr> <chr>               <dbl>
##  1 PC1   elev_m           4.37e-35
##  2 PC1   Season:elev_m    2.04e-18
##  3 PC1   Season           5.22e-16
##  4 PC1   Lat              3.81e-15
##  5 PC1   timeframe        6.31e- 4
##  6 PC1   elev_m:Lat       1.72e- 2
##  7 PC1   timeframe:elev_m 1.86e- 2
##  8 PC1   Season:Lat       3.08e- 2
##  9 PC10  Lat              4.46e- 4
## 10 PC10  timeframe:elev_m 6.89e- 3
## # ℹ 48 more rows
#PC1 has sig timeframe*elev_m effect too

lmer_results_allclim %>% select(-data, -glm) %>% unnest(anova) %>% filter(PC=="PC1" | PC=="PC2" |PC=="PC3" | PC=="PC4") 
## # A tibble: 64 × 8
## # Groups:   PC [4]
##    PC    term      df deviance df.residual residual.deviance statistic   p.value
##    <chr> <chr>  <int>    <dbl>       <int>             <dbl>     <dbl>     <dbl>
##  1 PC1   NULL      NA  NA               91             431.    NA      NA       
##  2 PC1   Season     1  48.2             90             383.   105.      5.22e-16
##  3 PC1   timef…     1   5.82            89             377.    12.7     6.31e- 4
##  4 PC1   elev_m     1 228.              88             150.   497.      4.37e-35
##  5 PC1   Lat        1  44.0             87             106.    96.2     3.81e-15
##  6 PC1   Seaso…     1   0.128           86             106.     0.279   5.99e- 1
##  7 PC1   Seaso…     1  61.2             85              44.4  134.      2.04e-18
##  8 PC1   timef…     1   2.65            84              41.8    5.78    1.86e- 2
##  9 PC1   Seaso…     1   2.22            83              39.6    4.84    3.08e- 2
## 10 PC1   timef…     1   0.0224          82              39.5    0.0489  8.26e- 1
## # ℹ 54 more rows

Plot climate change PCA

autoplot(all_clim_avgs.pc, data = all_clim_avgs,
         x=1, y=2,
         colour='elev_m', alpha=0.5,
         label=TRUE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

home_sites_pca_allclim <- all_clim_avgs_locs.pc %>%  
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC1, y=PC2, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  labs(x="PC1 (36.47%)", y="PC2 (25.94%)", color="Elevation (m)") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .9) +
  annotate("text", x = -6.4, y = -5, label = "Cold", size=7) +
  annotate("text", x = 3.68, y = -5, label = "Warm", size=7) +
  coord_cartesian(ylim = c(-4.9, 7), xlim = c(-6.8,4.5), clip = "off") +
  theme_classic() +
  theme(text=element_text(size=28)) +
  facet_wrap(~Season)

## add WL2 garden 2023 and 2024
WL2GRDN_allclim_pc_prep_2023_grwssn <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2023, Season=="Growth Season") %>% 
  select(cwd:ppt, ann_tmean:ppt_coldest_month) 
WL2GRDN_allclim_predicted_2023_grwssn <- predict(all_clim_avgs.pc, newdata = WL2GRDN_allclim_pc_prep_2023_grwssn)
WL2GRDN_allclim_pc_prep_2023_wtryr <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2023, Season=="Water Year") %>% 
  select(cwd:ppt, ann_tmean:ppt_coldest_month) 
WL2GRDN_allclim_predicted_2023_wtryr <- predict(all_clim_avgs.pc, newdata = WL2GRDN_allclim_pc_prep_2023_wtryr)

WL2GRDN_allclim_pc_prep_2024_grwssn <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2024, Season=="Growth Season") %>% 
  select(cwd:ppt, ann_tmean:ppt_coldest_month) 
WL2GRDN_allclim_predicted_2024_grwssn <- predict(all_clim_avgs.pc, newdata = WL2GRDN_allclim_pc_prep_2024_grwssn)
WL2GRDN_allclim_pc_prep_2024_wtryr <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2024, Season=="Water Year") %>% 
  select(cwd:ppt, ann_tmean:ppt_coldest_month) 
WL2GRDN_allclim_predicted_2024_wtryr <- predict(all_clim_avgs.pc, newdata = WL2GRDN_allclim_pc_prep_2024_wtryr)

home_sites_pca_allclim$data <- rbind(home_sites_pca_allclim$data, 
  data.frame(
    parent.pop = "WL2_Garden",
    elev_m = 2020,
    Lat = NA,
    timeframe = c("2023", "2023", "2024", "2024"),
    Season = c("Growth Season", "Water Year", "Growth Season", "Water Year"),
    PC1 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC1"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC1"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC1"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC1"] ),
    PC2 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC2"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC2"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC2"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC2"] ),
    PC3 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC3"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC3"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC3"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC3"] ),
    PC4 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC4"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC4"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC4"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC4"] ),
    PC5 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC5"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC5"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC5"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC5"] ),
    PC6 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC6"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC6"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC6"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC6"] ),
    PC7 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC7"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC7"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC7"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC7"] ),
    PC8 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC8"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC8"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC8"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC8"] ),
    PC9 =  c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC9"],  WL2GRDN_allclim_predicted_2023_wtryr[, "PC9"] , WL2GRDN_allclim_predicted_2024_grwssn[, "PC9"],  WL2GRDN_allclim_predicted_2024_wtryr[, "PC9"] ),
    PC10 = c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC10"], WL2GRDN_allclim_predicted_2023_wtryr[, "PC10"], WL2GRDN_allclim_predicted_2024_grwssn[, "PC10"], WL2GRDN_allclim_predicted_2024_wtryr[, "PC10"]),
    PC11 = c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC11"], WL2GRDN_allclim_predicted_2023_wtryr[, "PC11"], WL2GRDN_allclim_predicted_2024_grwssn[, "PC11"], WL2GRDN_allclim_predicted_2024_wtryr[, "PC11"]),
    PC12 = c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC12"], WL2GRDN_allclim_predicted_2023_wtryr[, "PC12"], WL2GRDN_allclim_predicted_2024_grwssn[, "PC12"], WL2GRDN_allclim_predicted_2024_wtryr[, "PC12"]),
    PC13 = c(WL2GRDN_allclim_predicted_2023_grwssn[, "PC13"], WL2GRDN_allclim_predicted_2023_wtryr[, "PC13"], WL2GRDN_allclim_predicted_2024_grwssn[, "PC13"], WL2GRDN_allclim_predicted_2024_wtryr[, "PC13"]),
    group = c("new", "new2", "new3", "new4")
  )
)

Y_label <- data.frame(
  label = c("Dry", "Wet"),
  Season = c("Growth Season", "Growth Season"),
  x     = c(-6.7, -6.7),
  y     = c(-3.75, 6.2),
  timeframe = NA   
)
WL2_label <- data.frame(
  label = c("WL2", "WL2",
            "WL2 Garden \n 2023", "WL2 Garden \n 2024",
            "WL2 \n Garden \n 2023", "WL2 \n Garden \n 2024"),
  Season   = c("Growth Season", "Water Year",
               "Growth Season", "Growth Season",
               "Water Year", "Water Year"),
  x     = c(1.6, -1.5,
            -1.2, 2.5,
            -6.0, -0.2),
  y     = c(-0.68, 1.9,
            -1.2, -3.5,
            5, 0.2),
  timeframe = NA
)

home_sites_pca_allclim + 
  geom_point(data=filter(home_sites_pca_allclim$data, parent.pop == "WL2_Garden"), 
             size=6, shape = 18, show.legend = FALSE) +
  geom_text(data = WL2_label,
  mapping = aes(x = x, y = y, label = label),
  colour = "purple",
  size=7
) +
  geom_text(data = Y_label,
  mapping = aes(x = x, y = y, label = label),
  colour = "black",
  size=7
)

ggsave("../Figures/AllClim_PC1-PC2_PlusGarden.png", width = 14.8, height = 6, units = "in")

Water Year PCA

wtr_yr_avgs_normalized <- all_clim_avgs %>% 
  filter(Season=="Water Year") %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_wtr_yr = cor(wtr_yr_avgs_normalized) #test correlations among the traits
cor.sig_wtr_yr <- cor.mtest(wtr_yr_avgs_normalized, method = "pearson") #get pearson's test p-values 
cor.norm_wtr_yr
##                             cwd        pck        ppt        tmn        tmx
## cwd                  1.00000000 -0.2778199 -0.6334849  0.1824027  0.2293903
## pck                 -0.27781989  1.0000000  0.7036374 -0.8628664 -0.8453649
## ppt                 -0.63348487  0.7036374  1.0000000 -0.4493381 -0.4916682
## tmn                  0.18240273 -0.8628664 -0.4493381  1.0000000  0.9792912
## tmx                  0.22939032 -0.8453649 -0.4916682  0.9792912  1.0000000
## ann_tmean            0.20783921 -0.8582421 -0.4737383  0.9944189  0.9951856
## mean_diurnal_range   0.28734808 -0.2099630 -0.3559296  0.2412914  0.4327692
## temp_seasonality     0.08535835 -0.4585397 -0.2927491  0.3724661  0.3709750
## temp_ann_range       0.24767575 -0.1840337 -0.3117025  0.2093605  0.3847761
## tmean_wettest_month  0.19445528 -0.8087459 -0.4334553  0.9775323  0.9901544
## tmean_driest_month   0.15228885 -0.8208410 -0.3849627  0.9861790  0.9836271
## ann_ppt             -0.63348487  0.7036374  1.0000000 -0.4493381 -0.4916682
## ppt_seasonality      0.35247991 -0.7114081 -0.5116831  0.7057181  0.6429981
## ppt_warmest_month   -0.21384284  0.8484319  0.4213677 -0.9273903 -0.8974292
## ppt_coldest_month   -0.51790365  0.4739895  0.7853312 -0.4176279 -0.5317284
##                      ann_tmean mean_diurnal_range temp_seasonality
## cwd                  0.2078392         0.28734808       0.08535835
## pck                 -0.8582421        -0.20996305      -0.45853965
## ppt                 -0.4737383        -0.35592963      -0.29274913
## tmn                  0.9944189         0.24129144       0.37246610
## tmx                  0.9951856         0.43276917       0.37097502
## ann_tmean            1.0000000         0.34233107       0.37362985
## mean_diurnal_range   0.3423311         1.00000000       0.11969846
## temp_seasonality     0.3736298         0.11969846       1.00000000
## temp_ann_range       0.3018635         0.91213380       0.44828421
## tmean_wettest_month  0.9892034         0.39340777       0.29312354
## tmean_driest_month   0.9899877         0.32361760       0.41059293
## ann_ppt             -0.4737383        -0.35592963      -0.29274913
## ppt_seasonality      0.6767109        -0.06030398       0.41330610
## ppt_warmest_month   -0.9166093        -0.17221356      -0.38378663
## ppt_coldest_month   -0.4792638        -0.68915334      -0.07076714
##                     temp_ann_range tmean_wettest_month tmean_driest_month
## cwd                     0.24767575           0.1944553          0.1522889
## pck                    -0.18403374          -0.8087459         -0.8208410
## ppt                    -0.31170245          -0.4334553         -0.3849627
## tmn                     0.20936046           0.9775323          0.9861790
## tmx                     0.38477608           0.9901544          0.9836271
## ann_tmean               0.30186346           0.9892034          0.9899877
## mean_diurnal_range      0.91213380           0.3934078          0.3236176
## temp_seasonality        0.44828421           0.2931235          0.4105929
## temp_ann_range          1.00000000           0.3296974          0.3068072
## tmean_wettest_month     0.32969743           1.0000000          0.9801624
## tmean_driest_month      0.30680720           0.9801624          1.0000000
## ann_ppt                -0.31170245          -0.4334553         -0.3849627
## ppt_seasonality        -0.01923439           0.5950874          0.6422224
## ppt_warmest_month      -0.15552992          -0.8844125         -0.9164854
## ppt_coldest_month      -0.58703197          -0.5171949         -0.4098137
##                        ann_ppt ppt_seasonality ppt_warmest_month
## cwd                 -0.6334849      0.35247991        -0.2138428
## pck                  0.7036374     -0.71140813         0.8484319
## ppt                  1.0000000     -0.51168311         0.4213677
## tmn                 -0.4493381      0.70571809        -0.9273903
## tmx                 -0.4916682      0.64299814        -0.8974292
## ann_tmean           -0.4737383      0.67671087        -0.9166093
## mean_diurnal_range  -0.3559296     -0.06030398        -0.1722136
## temp_seasonality    -0.2927491      0.41330610        -0.3837866
## temp_ann_range      -0.3117025     -0.01923439        -0.1555299
## tmean_wettest_month -0.4334553      0.59508741        -0.8844125
## tmean_driest_month  -0.3849627      0.64222243        -0.9164854
## ann_ppt              1.0000000     -0.51168311         0.4213677
## ppt_seasonality     -0.5116831      1.00000000        -0.7785066
## ppt_warmest_month    0.4213677     -0.77850659         1.0000000
## ppt_coldest_month    0.7853312     -0.15019686         0.2645227
##                     ppt_coldest_month
## cwd                       -0.51790365
## pck                        0.47398947
## ppt                        0.78533118
## tmn                       -0.41762794
## tmx                       -0.53172838
## ann_tmean                 -0.47926375
## mean_diurnal_range        -0.68915334
## temp_seasonality          -0.07076714
## temp_ann_range            -0.58703197
## tmean_wettest_month       -0.51719490
## tmean_driest_month        -0.40981374
## ann_ppt                    0.78533118
## ppt_seasonality           -0.15019686
## ppt_warmest_month          0.26452270
## ppt_coldest_month          1.00000000
cor.sig_wtr_yr$p
##                              cwd          pck          ppt          tmn
## cwd                 0.000000e+00 6.156743e-02 2.298434e-06 2.250231e-01
## pck                 6.156743e-02 0.000000e+00 4.916374e-08 1.258286e-14
## ppt                 2.298434e-06 4.916374e-08 0.000000e+00 1.732637e-03
## tmn                 2.250231e-01 1.258286e-14 1.732637e-03 0.000000e+00
## tmx                 1.251492e-01 1.464187e-13 5.202761e-04 3.673162e-32
## ann_tmean           1.657380e-01 2.483756e-14 8.825116e-04 1.270104e-44
## mean_diurnal_range  5.282989e-02 1.613590e-01 1.519546e-02 1.062132e-01
## temp_seasonality    5.727357e-01 1.351257e-03 4.833963e-02 1.079951e-02
## temp_ann_range      9.700324e-02 2.208328e-01 3.496703e-02 1.625928e-01
## tmean_wettest_month 1.953319e-01 1.053930e-11 2.619418e-03 2.168711e-31
## tmean_driest_month  3.123245e-01 2.859308e-12 8.247654e-03 5.392913e-36
## ann_ppt             2.298434e-06 4.916374e-08 0.000000e+00 1.732637e-03
## ppt_seasonality     1.628287e-02 2.998251e-08 2.784782e-04 4.313300e-08
## ppt_warmest_month   1.535783e-01 9.740293e-14 3.541662e-03 2.090112e-20
## ppt_coldest_month   2.274928e-04 8.761763e-04 1.032132e-10 3.879606e-03
##                              tmx    ann_tmean mean_diurnal_range
## cwd                 1.251492e-01 1.657380e-01       5.282989e-02
## pck                 1.464187e-13 2.483756e-14       1.613590e-01
## ppt                 5.202761e-04 8.825116e-04       1.519546e-02
## tmn                 3.673162e-32 1.270104e-44       1.062132e-01
## tmx                 0.000000e+00 4.957242e-46       2.665434e-03
## ann_tmean           4.957242e-46 0.000000e+00       1.987202e-02
## mean_diurnal_range  2.665434e-03 1.987202e-02       0.000000e+00
## temp_seasonality    1.114505e-02 1.053625e-02       4.281583e-01
## temp_ann_range      8.281539e-03 4.146731e-02       1.183814e-18
## tmean_wettest_month 3.226246e-39 2.429382e-38       6.834275e-03
## tmean_driest_month  2.185193e-34 4.659562e-39       2.824338e-02
## ann_ppt             5.202761e-04 8.825116e-04       1.519546e-02
## ppt_seasonality     1.444691e-06 2.431457e-07       6.905504e-01
## ppt_warmest_month   3.052513e-17 3.927396e-19       2.524411e-01
## ppt_coldest_month   1.430891e-04 7.521866e-04       1.186502e-07
##                     temp_seasonality temp_ann_range tmean_wettest_month
## cwd                      0.572735694   9.700324e-02        1.953319e-01
## pck                      0.001351257   2.208328e-01        1.053930e-11
## ppt                      0.048339634   3.496703e-02        2.619418e-03
## tmn                      0.010799513   1.625928e-01        2.168711e-31
## tmx                      0.011145053   8.281539e-03        3.226246e-39
## ann_tmean                0.010536252   4.146731e-02        2.429382e-38
## mean_diurnal_range       0.428158267   1.183814e-18        6.834275e-03
## temp_seasonality         0.000000000   1.781903e-03        4.804021e-02
## temp_ann_range           0.001781903   0.000000e+00        2.525042e-02
## tmean_wettest_month      0.048040207   2.525042e-02        0.000000e+00
## tmean_driest_month       0.004592583   3.808751e-02        1.439550e-32
## ann_ppt                  0.048339634   3.496703e-02        2.619418e-03
## ppt_seasonality          0.004305085   8.990390e-01        1.289856e-05
## ppt_warmest_month        0.008463232   3.020148e-01        3.684748e-16
## ppt_coldest_month        0.640253893   1.801670e-05        2.328410e-04
##                     tmean_driest_month      ann_ppt ppt_seasonality
## cwd                       3.123245e-01 2.298434e-06    1.628287e-02
## pck                       2.859308e-12 4.916374e-08    2.998251e-08
## ppt                       8.247654e-03 0.000000e+00    2.784782e-04
## tmn                       5.392913e-36 1.732637e-03    4.313300e-08
## tmx                       2.185193e-34 5.202761e-04    1.444691e-06
## ann_tmean                 4.659562e-39 8.825116e-04    2.431457e-07
## mean_diurnal_range        2.824338e-02 1.519546e-02    6.905504e-01
## temp_seasonality          4.592583e-03 4.833963e-02    4.305085e-03
## temp_ann_range            3.808751e-02 3.496703e-02    8.990390e-01
## tmean_wettest_month       1.439550e-32 2.619418e-03    1.289856e-05
## tmean_driest_month        0.000000e+00 8.247654e-03    1.501332e-06
## ann_ppt                   8.247654e-03 0.000000e+00    2.784782e-04
## ppt_seasonality           1.501332e-06 2.784782e-04    0.000000e+00
## ppt_warmest_month         4.052632e-19 3.541662e-03    1.904144e-10
## ppt_coldest_month         4.678189e-03 1.032132e-10    3.190965e-01
##                     ppt_warmest_month ppt_coldest_month
## cwd                      1.535783e-01      2.274928e-04
## pck                      9.740293e-14      8.761763e-04
## ppt                      3.541662e-03      1.032132e-10
## tmn                      2.090112e-20      3.879606e-03
## tmx                      3.052513e-17      1.430891e-04
## ann_tmean                3.927396e-19      7.521866e-04
## mean_diurnal_range       2.524411e-01      1.186502e-07
## temp_seasonality         8.463232e-03      6.402539e-01
## temp_ann_range           3.020148e-01      1.801670e-05
## tmean_wettest_month      3.684748e-16      2.328410e-04
## tmean_driest_month       4.052632e-19      4.678189e-03
## ann_ppt                  3.541662e-03      1.032132e-10
## ppt_seasonality          1.904144e-10      3.190965e-01
## ppt_warmest_month        0.000000e+00      7.565041e-02
## ppt_coldest_month        7.565041e-02      0.000000e+00
#ann_ppt and ppt 100% correlated (ppt = avg across monts, ann_ppt = avg of the total ppt in a year) - only keep ann_ppt 
#tmn, tmx, tmean_wettest_month, tmean_driest_month and ann_tmean all highly correlated (97-99%) - only keep ann_tmean 
#ppt warmest month highly neg correlated with tmn, ann_tmean, tmean_driest - take it out 
#temp ann range and mean diurnal range highly corr - keep temp_ann_range

wtr_yr_avgs <- all_clim_avgs %>% 
  filter(Season=="Water Year") %>% 
  ungroup()
wtr_yr_avgs.pc = prcomp(wtr_yr_avgs[c(8:9, 13, 15:16, 19:20, 22)], scale = TRUE, center = TRUE)
summary(wtr_yr_avgs.pc)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6     PC7
## Standard deviation     2.0049 1.2082 1.0645 0.84647 0.61935 0.43145 0.29412
## Proportion of Variance 0.5025 0.1825 0.1417 0.08956 0.04795 0.02327 0.01081
## Cumulative Proportion  0.5025 0.6849 0.8266 0.91615 0.96410 0.98737 0.99818
##                            PC8
## Standard deviation     0.12057
## Proportion of Variance 0.00182
## Cumulative Proportion  1.00000
tibble(PC=str_c("PC",str_pad(1:8,2,pad="0")),
       percent_var=wtr_yr_avgs.pc$sdev[1:8]^2/sum(wtr_yr_avgs.pc$sdev^2)*100) %>%
  ggplot(aes(x=PC, y=percent_var)) +
  geom_col() +
  ggtitle("Percent Variance Explained")

#combine pcs with metadata
wtr_yr_avgs.pc.dat = data.frame(wtr_yr_avgs.pc$x)
wtr_yr_avgs_locs.pc = cbind(wtr_yr_avgs, wtr_yr_avgs.pc.dat)
wtr_yr_avgs_loadings = data.frame(varnames=rownames(wtr_yr_avgs.pc$rotation), wtr_yr_avgs.pc$rotation)
wtr_yr_avgs_loadings
##                            varnames        PC1        PC2         PC3
## cwd                             cwd -0.2883385 -0.3473952  0.37741110
## pck                             pck  0.4349608 -0.2904655 -0.05643197
## ann_tmean                 ann_tmean -0.4033523  0.2757794 -0.05398867
## temp_seasonality   temp_seasonality -0.2581799  0.2512527 -0.60426764
## temp_ann_range       temp_ann_range -0.2416149 -0.4173999 -0.61961564
## ann_ppt                     ann_ppt  0.4292284  0.1965747 -0.23663127
## ppt_seasonality     ppt_seasonality -0.3511527  0.4548416  0.20883693
## ppt_coldest_month ppt_coldest_month  0.3638642  0.4858179 -0.05283346
##                           PC4        PC5        PC6         PC7         PC8
## cwd                0.57743481  0.3905207  0.4062419 -0.02610130  0.04568685
## pck                0.23720501  0.2233428 -0.3558512  0.56028891 -0.42018138
## ann_tmean         -0.43696683  0.4240691  0.2847712  0.29722857 -0.46703730
## temp_seasonality   0.48995528 -0.3030735  0.2041767  0.35765103  0.05969711
## temp_ann_range    -0.02817636  0.3457510 -0.2657792 -0.43403266 -0.06415091
## ann_ppt           -0.08541412  0.5560173  0.2230052  0.13600852  0.58004018
## ppt_seasonality    0.23163337  0.2785809 -0.6713289  0.04761772  0.20490668
## ppt_coldest_month  0.34287483  0.1400611  0.1204255 -0.50988809 -0.46596571

Permanova

wtr_yr_avgs_locs.pc_dist <- wtr_yr_avgs_locs.pc %>% ungroup() %>% select(PC1:PC8)
dist_matrix_wtr_year <- dist(wtr_yr_avgs_locs.pc_dist, method = "euclidian") #use a distance function to calculate euclidian distance in PCA space
permanova_results_wtr_year <- adonis2(dist_matrix_wtr_year ~ timeframe*elev_m*Lat, data = wtr_yr_avgs_locs.pc) #use adonis2 to run the permanova
permanova_results_wtr_year #look at output 
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = dist_matrix_wtr_year ~ timeframe * elev_m * Lat, data = wtr_yr_avgs_locs.pc)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     7   231.29 0.64246 9.7547  0.001 ***
## Residual 38   128.71 0.35754                  
## Total    45   360.00 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#get stats per term in the model:
permanova_results_wtr_year_terms <- adonis2(dist_matrix_wtr_year ~ timeframe*elev_m*Lat, data = wtr_yr_avgs_locs.pc, by = "terms")
permanova_results_wtr_year_terms
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = dist_matrix_wtr_year ~ timeframe * elev_m * Lat, data = wtr_yr_avgs_locs.pc, by = "terms")
##                      Df SumOfSqs      R2       F Pr(>F)    
## timeframe             1    32.63 0.09064  9.6338  0.001 ***
## elev_m                1   122.98 0.34161 36.3073  0.001 ***
## Lat                   1    58.94 0.16373 17.4016  0.001 ***
## timeframe:elev_m      1     3.25 0.00903  0.9595  0.412    
## timeframe:Lat         1     3.64 0.01012  1.0754  0.345    
## elev_m:Lat            1     9.52 0.02644  2.8100  0.030 *  
## timeframe:elev_m:Lat  1     0.32 0.00089  0.0951  0.994    
## Residual             38   128.71 0.35754                   
## Total                45   360.00 1.00000                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#LM on PCs (follow up on permanova)
lmer_results_wtr_year <- wtr_yr_avgs_locs.pc %>%
  ungroup() %>% 
  select(timeframe, parent.pop, elev_m, Lat, Long, PC1:PC8) %>% 
  pivot_longer(starts_with("PC", ignore.case = FALSE), 
               names_to = "PC", values_to = "value") %>% 
  group_by(PC) %>% 
  nest(data=c(timeframe, parent.pop, elev_m, Lat, Long, value)) %>% 
  mutate(glm=map(data, ~ glm(value ~ timeframe*elev_m*Lat,
                               data=.x)),
         anova = map(glm, ~ broom.mixed::tidy(anova(.x))))

PC_anova_wtr_yr <- lmer_results_wtr_year %>% select(-data, -glm) %>% unnest(anova) %>%
  select(PC, term, p.value) %>%
  filter(p.value < 0.05) %>%
  arrange(term, p.value)
PC_anova_wtr_yr #PC2 and PC4 most sig, PC1 and PC7 also have sig timeframe effects 
## # A tibble: 12 × 3
## # Groups:   PC [6]
##    PC    term        p.value
##    <chr> <chr>         <dbl>
##  1 PC3   Lat        1.90e- 7
##  2 PC1   Lat        2.77e- 7
##  3 PC5   Lat        5.79e- 3
##  4 PC1   elev_m     4.61e-14
##  5 PC4   elev_m     1.02e- 4
##  6 PC5   elev_m     3.44e- 2
##  7 PC7   elev_m:Lat 8.07e- 4
##  8 PC5   elev_m:Lat 2.63e- 3
##  9 PC4   timeframe  7.35e- 6
## 10 PC2   timeframe  2.83e- 4
## 11 PC7   timeframe  6.63e- 3
## 12 PC1   timeframe  3.21e- 2
lmer_results_wtr_year %>% select(-data, -glm) %>% unnest(anova) %>% filter(PC=="PC1" | PC=="PC2" |PC=="PC3" | PC=="PC4") 
## # A tibble: 32 × 8
## # Groups:   PC [4]
##    PC    term      df deviance df.residual residual.deviance statistic   p.value
##    <chr> <chr>  <int>    <dbl>       <int>             <dbl>     <dbl>     <dbl>
##  1 PC1   NULL      NA  NA               45             181.    NA      NA       
##  2 PC1   timef…     1   4.07            44             177.     4.95    3.21e- 2
##  3 PC1   elev_m     1 111.              43              66.2  135.      4.61e-14
##  4 PC1   Lat        1  31.8             42              34.4   38.8     2.77e- 7
##  5 PC1   timef…     1   0.651           41              33.7    0.793   3.79e- 1
##  6 PC1   timef…     1   0.550           40              33.1    0.671   4.18e- 1
##  7 PC1   elev_…     1   1.92            39              31.2    2.34    1.34e- 1
##  8 PC1   timef…     1   0.0365          38              31.2    0.0445  8.34e- 1
##  9 PC2   NULL      NA  NA               45              65.7   NA      NA       
## 10 PC2   timef…     1  17.6             44              48.1   16.0     2.83e- 4
## # ℹ 22 more rows

Plot climate change PCA

#prep the pc for making a plot with arrows distinguishing recent vs. historical time 
wtr_yr_avgs_locs.pc_avg <- wtr_yr_avgs_locs.pc %>%
  group_by(parent.pop, elev_m, timeframe) %>%
  summarise(across(.cols=starts_with("PC", ignore.case = FALSE), .fns = mean)) %>%
  ungroup()
## `summarise()` has grouped output by 'parent.pop', 'elev_m'. You can override
## using the `.groups` argument.
autoplot(wtr_yr_avgs.pc, data = wtr_yr_avgs,
         x=1, y=2,
         colour='elev_m', alpha=0.5,
         label=TRUE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

autoplot(wtr_yr_avgs.pc, data = wtr_yr_avgs,
         x=2, y=4,
         colour='elev_m', alpha=0.5,
         label=TRUE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

autoplot(wtr_yr_avgs.pc, data = wtr_yr_avgs,
         x=1, y=4,
         colour='elev_m', alpha=0.5,
         label=TRUE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

wtr_yr_avgs_locs.pc_avg %>% 
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC1, y=PC2, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_point(size=2, alpha=0.7) +
  geom_text_repel(aes(label = parent.pop)) +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed")  +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8)

wtr_yr_avgs_locs.pc_avg %>% 
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC2, y=PC4, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_point(size=2, alpha=0.7) +
  geom_text_repel(aes(label = parent.pop)) +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed")  +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8)

wtr_yr_avgs_locs.pc_avg %>% 
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC1, y=PC4, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_point(size=2, alpha=0.7) +
  geom_text_repel(aes(label = parent.pop)) +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed")  +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8)

#wtr_yr_avgs_locs.pc_avg %>% filter(parent.pop=="WL2")
#wtr_yr_avgs_loadings
#PC1 = positive PPT and PCK, negative ANN_TMEAN
#PC2 = positive ppt_seasonality and ppt_coldest month, negative temp_ann_range
#PC4 = positive CWD and temp_seasonality and negative ann_tmean
home_sites_pca_wtryr <- wtr_yr_avgs_locs.pc_avg %>%  
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC1, y=PC2, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  labs(x="PC1 (50.25%)", y="PC2 (18.25%)", color="Elevation (m)") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  annotate("text", x = 1.3, y= 1.16, label = "WL2", colour = "purple", size=7) +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8) +
  annotate("text", x = -4.5, y = -5.4, label = "Warm \n No Snow", size=6) +
  annotate("text", x = 4.5, y = -5.4, label = "Cold \n Snow", size=6) +
  annotate("text", x = -6.2, y = -3, label = "Low PPT \n Seasonality", size=6) +
  annotate("text", x = -6.2, y = 3, label = "High PPT \n Seasonality", size=6) +
  coord_cartesian(ylim = c(-4, 4), xlim = c(-5,5), clip = "off") +
  theme_classic() +
  theme(text=element_text(size=28))

## add WL2 garden 2023 and 2024
WL2GRDN_wtryr_pc_prep_2023 <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2023, Season=="Water Year") %>% 
  select(cwd:pck, ann_tmean, temp_seasonality, temp_ann_range, ann_ppt, ppt_seasonality, ppt_coldest_month) 
WL2GRDN_wtryr_predicted_2023 <- predict(wtr_yr_avgs.pc, newdata = WL2GRDN_wtryr_pc_prep_2023)

WL2GRDN_wtryr_pc_prep_2024 <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2024, Season=="Water Year") %>% 
  select(cwd:pck, ann_tmean, temp_seasonality, temp_ann_range, ann_ppt, ppt_seasonality, ppt_coldest_month) 
WL2GRDN_wtryr_2024_predicted <- predict(wtr_yr_avgs.pc, newdata = WL2GRDN_wtryr_pc_prep_2024)

home_sites_pca_wtryr$data <- rbind(home_sites_pca_wtryr$data, 
  data.frame(
    parent.pop = "WL2_Garden",
    elev_m = 2020,
    timeframe = c("2023", "2024"),
    PC1 = c(WL2GRDN_wtryr_predicted_2023[, "PC1"], WL2GRDN_wtryr_2024_predicted[, "PC1"]),
    PC2 = c(WL2GRDN_wtryr_predicted_2023[, "PC2"], WL2GRDN_wtryr_2024_predicted[, "PC2"]),
    PC3 = c(WL2GRDN_wtryr_predicted_2023[, "PC3"], WL2GRDN_wtryr_2024_predicted[, "PC3"]),
    PC4 = c(WL2GRDN_wtryr_predicted_2023[, "PC4"], WL2GRDN_wtryr_2024_predicted[, "PC4"]),
    PC5 = c(WL2GRDN_wtryr_predicted_2023[, "PC5"], WL2GRDN_wtryr_2024_predicted[, "PC5"]),
    PC6 = c(WL2GRDN_wtryr_predicted_2023[, "PC6"], WL2GRDN_wtryr_2024_predicted[, "PC6"]),
    PC7 = c(WL2GRDN_wtryr_predicted_2023[, "PC7"], WL2GRDN_wtryr_2024_predicted[, "PC7"]),
    PC8 = c(WL2GRDN_wtryr_predicted_2023[, "PC8"], WL2GRDN_wtryr_2024_predicted[, "PC8"]),
    group = c("new", "new2")
  )
)

home_sites_pca_wtryr + 
  geom_point(data=filter(home_sites_pca_wtryr$data, parent.pop == "WL2_Garden"), size=6, shape = 18, show.legend = FALSE) +
  annotate("text", x = 4.49, y= 2.7, label = "WL2 \n Garden \n 2023", colour = "purple", size=6) +
  annotate("text", x = -0.71, y= 1.9, label = "WL2 \n Garden \n 2024", colour = "purple", size=6) 

ggsave("../Figures/Wtr_Year_PC1-PC2_PlusGarden.png", width = 7.4, height = 6, units = "in")

Growth Season PCA

grwssn_avgs_normalized <- all_clim_avgs %>% 
  filter(Season=="Growth Season") %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_grwssn = cor(grwssn_avgs_normalized) #test correlations among the traits
cor.sig_grwssn <- cor.mtest(grwssn_avgs_normalized, method = "pearson") #get pearson's test p-values 
cor.norm_grwssn
##                             cwd        pck         ppt        tmn         tmx
## cwd                  1.00000000  0.2451066 -0.72857067 -0.3682706 -0.33727778
## pck                  0.24510659  1.0000000  0.13194309 -0.8378476 -0.77049664
## ppt                 -0.72857067  0.1319431  1.00000000  0.1254422  0.15268049
## tmn                 -0.36827062 -0.8378476  0.12544222  1.0000000  0.91377238
## tmx                 -0.33727778 -0.7704966  0.15268049  0.9137724  1.00000000
## ann_tmean           -0.36120137 -0.8233208  0.14164723  0.9797849  0.97656763
## mean_diurnal_range   0.13466606  0.2991502  0.04055125 -0.3704043  0.03886672
## temp_seasonality     0.29743512 -0.2005587 -0.46077431  0.4019525  0.43396829
## temp_ann_range      -0.01041239 -0.6192414 -0.19084644  0.6804142  0.86403889
## tmean_wettest_month -0.59345820 -0.7715523  0.43757202  0.7495849  0.76191387
## tmean_driest_month  -0.49369595 -0.7826338  0.30437863  0.9280001  0.95402569
## ann_ppt             -0.73526218 -0.5147399  0.71140138  0.7325118  0.72943167
## ppt_seasonality      0.45598307  0.1806485 -0.43357048  0.1007506  0.03865398
## ppt_warmest_month    0.20339923  0.8549983  0.11170738 -0.9135610 -0.85180553
## ppt_coldest_month   -0.28231559  0.3261477  0.54381864  0.1574778  0.15001140
##                       ann_tmean mean_diurnal_range temp_seasonality
## cwd                 -0.36120137        0.134666060      0.297435123
## pck                 -0.82332083        0.299150223     -0.200558737
## ppt                  0.14164723        0.040551245     -0.460774305
## tmn                  0.97978485       -0.370404313      0.401952542
## tmx                  0.97656763        0.038866724      0.433968286
## ann_tmean            1.00000000       -0.177092153      0.426662476
## mean_diurnal_range  -0.17709215        1.000000000      0.003573292
## temp_seasonality     0.42666248        0.003573292      1.000000000
## temp_ann_range       0.78598247        0.301999527      0.683579289
## tmean_wettest_month  0.77233426       -0.101665082     -0.175366923
## tmean_driest_month   0.96146424       -0.101254889      0.267312613
## ann_ppt              0.74729280       -0.133941578      0.027897560
## ppt_seasonality      0.07241155       -0.159442800      0.731753824
## ppt_warmest_month   -0.90347441        0.299472091     -0.471146212
## ppt_coldest_month    0.15730462       -0.044353766      0.233607554
##                     temp_ann_range tmean_wettest_month tmean_driest_month
## cwd                    -0.01041239          -0.5934582         -0.4936959
## pck                    -0.61924138          -0.7715523         -0.7826338
## ppt                    -0.19084644           0.4375720          0.3043786
## tmn                     0.68041422           0.7495849          0.9280001
## tmx                     0.86403889           0.7619139          0.9540257
## ann_tmean               0.78598247           0.7723343          0.9614642
## mean_diurnal_range      0.30199953          -0.1016651         -0.1012549
## temp_seasonality        0.68357929          -0.1753669          0.2673126
## temp_ann_range          1.00000000           0.4572525          0.7322762
## tmean_wettest_month     0.45725255           1.0000000          0.8777016
## tmean_driest_month      0.73227618           0.8777016          1.0000000
## ann_ppt                 0.40566549           0.7780776          0.8168684
## ppt_seasonality         0.20038818          -0.5126221         -0.1363698
## ppt_warmest_month      -0.73452496          -0.6676225         -0.8500851
## ppt_coldest_month      -0.01213333          -0.1185820          0.1212578
##                         ann_ppt ppt_seasonality ppt_warmest_month
## cwd                 -0.73526218      0.45598307        0.20339923
## pck                 -0.51473990      0.18064845        0.85499827
## ppt                  0.71140138     -0.43357048        0.11170738
## tmn                  0.73251177      0.10075058       -0.91356095
## tmx                  0.72943167      0.03865398       -0.85180553
## ann_tmean            0.74729280      0.07241155       -0.90347441
## mean_diurnal_range  -0.13394158     -0.15944280        0.29947209
## temp_seasonality     0.02789756      0.73175382       -0.47114621
## temp_ann_range       0.40566549      0.20038818       -0.73452496
## tmean_wettest_month  0.77807755     -0.51262206       -0.66762252
## tmean_driest_month   0.81686835     -0.13636981       -0.85008506
## ann_ppt              1.00000000     -0.25108665       -0.55006980
## ppt_seasonality     -0.25108665      1.00000000       -0.17433133
## ppt_warmest_month   -0.55006980     -0.17433133        1.00000000
## ppt_coldest_month    0.45931956      0.41876861        0.02006087
##                     ppt_coldest_month
## cwd                       -0.28231559
## pck                        0.32614770
## ppt                        0.54381864
## tmn                        0.15747785
## tmx                        0.15001140
## ann_tmean                  0.15730462
## mean_diurnal_range        -0.04435377
## temp_seasonality           0.23360755
## temp_ann_range            -0.01213333
## tmean_wettest_month       -0.11858197
## tmean_driest_month         0.12125776
## ann_ppt                    0.45931956
## ppt_seasonality            0.41876861
## ppt_warmest_month          0.02006087
## ppt_coldest_month          1.00000000
cor.sig_grwssn$p
##                              cwd          pck          ppt          tmn
## cwd                 0.000000e+00 1.006324e-01 9.480850e-09 1.179598e-02
## pck                 1.006324e-01 0.000000e+00 3.820724e-01 3.835160e-13
## ppt                 9.480850e-09 3.820724e-01 0.000000e+00 4.061630e-01
## tmn                 1.179598e-02 3.835160e-13 4.061630e-01 0.000000e+00
## tmx                 2.189439e-02 3.803463e-10 3.110669e-01 7.959186e-19
## ann_tmean           1.365318e-02 2.162030e-12 3.477279e-01 2.171258e-32
## mean_diurnal_range  3.722387e-01 4.342401e-02 7.890281e-01 1.127979e-02
## temp_seasonality    4.469927e-02 1.814036e-01 1.270760e-03 5.622943e-03
## temp_ann_range      9.452455e-01 4.476642e-06 2.039150e-01 1.971043e-07
## tmean_wettest_month 1.381051e-05 3.477532e-10 2.357743e-03 2.046175e-09
## tmean_driest_month  4.891973e-04 1.318233e-10 3.971841e-02 1.747231e-20
## ann_ppt             5.910050e-09 2.522583e-04 2.999560e-08 7.189403e-09
## ppt_seasonality     1.448894e-03 2.295909e-01 2.611760e-03 5.052727e-01
## ppt_warmest_month   1.751701e-01 3.945830e-14 4.598440e-01 8.381202e-19
## ppt_coldest_month   5.731093e-02 2.696386e-02 9.380478e-05 2.959249e-01
##                              tmx    ann_tmean mean_diurnal_range
## cwd                 2.189439e-02 1.365318e-02         0.37223870
## pck                 3.803463e-10 2.162030e-12         0.04342401
## ppt                 3.110669e-01 3.477279e-01         0.78902807
## tmn                 7.959186e-19 2.171258e-32         0.01127979
## tmx                 0.000000e+00 5.415031e-31         0.79760401
## ann_tmean           5.415031e-31 0.000000e+00         0.23904532
## mean_diurnal_range  7.976040e-01 2.390453e-01         0.00000000
## temp_seasonality    2.585470e-03 3.107460e-03         0.98119693
## temp_ann_range      1.054861e-14 9.724161e-11         0.04137112
## tmean_wettest_month 7.745853e-10 3.253247e-10         0.50139580
## tmean_driest_month  1.183205e-24 2.629175e-26         0.50313286
## ann_ppt             8.928422e-09 2.436166e-09         0.37484018
## ppt_seasonality     7.986888e-01 6.324853e-01         0.28986284
## ppt_warmest_month   6.156080e-14 8.548681e-18         0.04318803
## ppt_coldest_month   3.197013e-01 2.964632e-01         0.76976130
##                     temp_seasonality temp_ann_range tmean_wettest_month
## cwd                     4.469927e-02   9.452455e-01        1.381051e-05
## pck                     1.814036e-01   4.476642e-06        3.477532e-10
## ppt                     1.270760e-03   2.039150e-01        2.357743e-03
## tmn                     5.622943e-03   1.971043e-07        2.046175e-09
## tmx                     2.585470e-03   1.054861e-14        7.745853e-10
## ann_tmean               3.107460e-03   9.724161e-11        3.253247e-10
## mean_diurnal_range      9.811969e-01   4.137112e-02        5.013958e-01
## temp_seasonality        0.000000e+00   1.643360e-07        2.437260e-01
## temp_ann_range          1.643360e-07   0.000000e+00        1.399654e-03
## tmean_wettest_month     2.437260e-01   1.399654e-03        0.000000e+00
## tmean_driest_month      7.250297e-02   7.310283e-09        1.187604e-15
## ann_ppt                 8.539829e-01   5.157823e-03        1.977458e-10
## ppt_seasonality         7.585129e-09   1.817829e-01        2.701735e-04
## ppt_warmest_month       9.503378e-04   6.230226e-09        4.019715e-07
## ppt_coldest_month       1.181686e-01   9.362131e-01        4.325101e-01
##                     tmean_driest_month      ann_ppt ppt_seasonality
## cwd                       4.891973e-04 5.910050e-09    1.448894e-03
## pck                       1.318233e-10 2.522583e-04    2.295909e-01
## ppt                       3.971841e-02 2.999560e-08    2.611760e-03
## tmn                       1.747231e-20 7.189403e-09    5.052727e-01
## tmx                       1.183205e-24 8.928422e-09    7.986888e-01
## ann_tmean                 2.629175e-26 2.436166e-09    6.324853e-01
## mean_diurnal_range        5.031329e-01 3.748402e-01    2.898628e-01
## temp_seasonality          7.250297e-02 8.539829e-01    7.585129e-09
## temp_ann_range            7.310283e-09 5.157823e-03    1.817829e-01
## tmean_wettest_month       1.187604e-15 1.977458e-10    2.701735e-04
## tmean_driest_month        0.000000e+00 4.435200e-12    3.661636e-01
## ann_ppt                   4.435200e-12 0.000000e+00    9.234216e-02
## ppt_seasonality           3.661636e-01 9.234216e-02    0.000000e+00
## ppt_warmest_month         7.789956e-14 7.492518e-05    2.465653e-01
## ppt_coldest_month         4.221219e-01 1.322662e-03    3.773652e-03
##                     ppt_warmest_month ppt_coldest_month
## cwd                      1.751701e-01      5.731093e-02
## pck                      3.945830e-14      2.696386e-02
## ppt                      4.598440e-01      9.380478e-05
## tmn                      8.381202e-19      2.959249e-01
## tmx                      6.156080e-14      3.197013e-01
## ann_tmean                8.548681e-18      2.964632e-01
## mean_diurnal_range       4.318803e-02      7.697613e-01
## temp_seasonality         9.503378e-04      1.181686e-01
## temp_ann_range           6.230226e-09      9.362131e-01
## tmean_wettest_month      4.019715e-07      4.325101e-01
## tmean_driest_month       7.789956e-14      4.221219e-01
## ann_ppt                  7.492518e-05      1.322662e-03
## ppt_seasonality          2.465653e-01      3.773652e-03
## ppt_warmest_month        0.000000e+00      8.947247e-01
## ppt_coldest_month        8.947247e-01      0.000000e+00
#tmn, tmx, tmean_driest_month and ann_tmean all highly correlated (90-98%) - only keep ann_tmean 
#ppt_warmest_month highly neg corr with tmn, ann_tmean - take it out 

grwssn_avgs <- all_clim_avgs %>% 
  filter(Season=="Growth Season") %>% 
  ungroup()
grwssn_avgs.pc = prcomp(grwssn_avgs[c(8:10, 13:17, 19:20, 22)], scale = TRUE, center = TRUE)
summary(grwssn_avgs.pc)
## Importance of components:
##                           PC1    PC2    PC3    PC4     PC5     PC6     PC7
## Standard deviation     2.0650 1.7146 1.4002 1.1146 0.54609 0.37391 0.29362
## Proportion of Variance 0.3876 0.2673 0.1782 0.1129 0.02711 0.01271 0.00784
## Cumulative Proportion  0.3876 0.6549 0.8331 0.9461 0.97318 0.98589 0.99373
##                            PC8     PC9    PC10    PC11
## Standard deviation     0.20060 0.12653 0.10516 0.04057
## Proportion of Variance 0.00366 0.00146 0.00101 0.00015
## Cumulative Proportion  0.99739 0.99884 0.99985 1.00000
tibble(PC=str_c("PC",str_pad(1:11,2,pad="0")),
       percent_var=grwssn_avgs.pc$sdev[1:11]^2/sum(grwssn_avgs.pc$sdev^2)*100) %>%
  ggplot(aes(x=PC, y=percent_var)) +
  geom_col() +
  ggtitle("Percent Variance Explained")

#combine pcs with metadata
grwssn_avgs.pc.dat = data.frame(grwssn_avgs.pc$x)
grwssn_avgs_locs.pc = cbind(grwssn_avgs, grwssn_avgs.pc.dat)
grwssn_avgs_loadings = data.frame(varnames=rownames(grwssn_avgs.pc$rotation), grwssn_avgs.pc$rotation)
grwssn_avgs_loadings
##                                varnames         PC1         PC2         PC3
## cwd                                 cwd  0.34735290  0.28128485  0.13039565
## pck                                 pck  0.35486850 -0.23817987 -0.33233268
## ppt                                 ppt -0.25886540 -0.40007940 -0.31081500
## ann_tmean                     ann_tmean -0.41670860  0.28025294 -0.01798899
## mean_diurnal_range   mean_diurnal_range  0.06793477 -0.00876756  0.02040727
## temp_seasonality       temp_seasonality -0.01061264  0.52475785 -0.23395704
## temp_ann_range           temp_ann_range -0.25821756  0.42908856  0.03980511
## tmean_wettest_month tmean_wettest_month -0.44900711 -0.04383096  0.21511238
## ann_ppt                         ann_ppt -0.45271873 -0.06190265 -0.21013037
## ppt_seasonality         ppt_seasonality  0.16624338  0.39869553 -0.39462543
## ppt_coldest_month     ppt_coldest_month -0.08458365 -0.03560988 -0.68755237
##                               PC4          PC5          PC6         PC7
## cwd                 -0.0418492646  0.827863235  0.302816094 -0.03010015
## pck                 -0.1982010790 -0.051012506  0.074657652 -0.51751706
## ppt                 -0.1163065695  0.210209948  0.130072306 -0.43377306
## ann_tmean            0.0477923303  0.163390973 -0.199000974 -0.06956301
## mean_diurnal_range  -0.8817906730  0.002429217 -0.242758592  0.18620414
## temp_seasonality    -0.0444375108 -0.347188547  0.510494316 -0.22972460
## temp_ann_range      -0.3597626954 -0.058404233  0.068452467 -0.11018798
## tmean_wettest_month -0.0154751276  0.244307534 -0.209323247 -0.36054287
## ann_ppt             -0.0079454364  0.109344823  0.289827979  0.16615026
## ppt_seasonality      0.1840971801  0.063456814 -0.627822821 -0.22002218
## ppt_coldest_month   -0.0007647201  0.203787351 -0.002925931  0.48236749
##                             PC8         PC9        PC10        PC11
## cwd                 -0.05091135  0.01442390  0.02173586 -0.01538845
## pck                  0.36172191 -0.02701541 -0.24277470 -0.44843945
## ppt                 -0.28024265 -0.25831211  0.33215622  0.39741296
## ann_tmean            0.41414633 -0.24213862  0.53408690 -0.39833454
## mean_diurnal_range  -0.14252875  0.21766423  0.22530657 -0.07260801
## temp_seasonality    -0.02219047  0.39174931  0.25874517  0.11392648
## temp_ann_range       0.09413240 -0.52876068 -0.50855069  0.22557106
## tmean_wettest_month  0.20287518  0.60826634 -0.28711941  0.15126549
## ann_ppt             -0.47589035  0.06949634 -0.25647143 -0.57127445
## ppt_seasonality     -0.41267243  0.03236594 -0.07313108 -0.02217544
## ppt_coldest_month    0.38589623  0.13193755 -0.12293171  0.25138980

Permanova

grwssn_avgs_locs.pc_dist <- grwssn_avgs_locs.pc %>% ungroup() %>% select(PC1:PC11)
dist_matrix_grwssn <- dist(grwssn_avgs_locs.pc_dist, method = "euclidian") #use a distance function to calculate euclidian distance in PCA space
permanova_results_grwssn <- adonis2(dist_matrix_grwssn ~ timeframe*elev_m*Lat, data = grwssn_avgs_locs.pc) #use adonis2 to run the permanova
permanova_results_grwssn #look at output 
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = dist_matrix_grwssn ~ timeframe * elev_m * Lat, data = grwssn_avgs_locs.pc)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     7   289.06 0.58396 7.6196  0.001 ***
## Residual 38   205.94 0.41604                  
## Total    45   495.00 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#get stats per term in the model:
permanova_results_grwssn_terms <- adonis2(dist_matrix_grwssn ~ timeframe*elev_m*Lat, data = grwssn_avgs_locs.pc, by = "terms")
permanova_results_grwssn_terms
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = dist_matrix_grwssn ~ timeframe * elev_m * Lat, data = grwssn_avgs_locs.pc, by = "terms")
##                      Df SumOfSqs      R2       F Pr(>F)    
## timeframe             1    18.71 0.03779  3.4520  0.012 *  
## elev_m                1   176.22 0.35600 32.5159  0.001 ***
## Lat                   1    59.16 0.11952 10.9164  0.001 ***
## timeframe:elev_m      1     9.27 0.01873  1.7106  0.155    
## timeframe:Lat         1     2.30 0.00464  0.4242  0.790    
## elev_m:Lat            1    22.53 0.04551  4.1567  0.004 ** 
## timeframe:elev_m:Lat  1     0.88 0.00177  0.1616  0.977    
## Residual             38   205.94 0.41604                   
## Total                45   495.00 1.00000                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#LM on PCs (follow up on permanova)
lmer_results_grwssn <- grwssn_avgs_locs.pc %>%
  ungroup() %>% 
  select(timeframe, parent.pop, elev_m, Lat, Long, PC1:PC11) %>% 
  pivot_longer(starts_with("PC", ignore.case = FALSE), 
               names_to = "PC", values_to = "value") %>% 
  group_by(PC) %>% 
  nest(data=c(timeframe, parent.pop, elev_m, Lat, Long, value)) %>% 
  mutate(glm=map(data, ~ glm(value ~ timeframe*elev_m*Lat,
                               data=.x)),
         anova = map(glm, ~ broom.mixed::tidy(anova(.x))))

PC_anova_grwssn <- lmer_results_grwssn %>% select(-data, -glm) %>% unnest(anova) %>%
  select(PC, term, p.value) %>%
  filter(p.value < 0.05) %>%
  arrange(term, p.value)
PC_anova_grwssn #PC2 and PC4 most sig, PC7 also has sig timeframe effects 
## # A tibble: 15 × 3
## # Groups:   PC [8]
##    PC    term                  p.value
##    <chr> <chr>                   <dbl>
##  1 PC4   Lat                  8.41e- 6
##  2 PC2   Lat                  1.90e- 3
##  3 PC3   Lat                  2.39e- 3
##  4 PC1   elev_m               4.22e-20
##  5 PC2   elev_m:Lat           1.85e- 3
##  6 PC8   elev_m:Lat           4.70e- 3
##  7 PC7   elev_m:Lat           6.19e- 3
##  8 PC10  elev_m:Lat           2.16e- 2
##  9 PC9   elev_m:Lat           3.35e- 2
## 10 PC4   timeframe            4.29e- 3
## 11 PC2   timeframe            2.82e- 2
## 12 PC7   timeframe            3.03e- 2
## 13 PC9   timeframe:Lat        4.04e- 3
## 14 PC2   timeframe:elev_m     4.75e- 2
## 15 PC9   timeframe:elev_m:Lat 2.15e- 2
#PC2 also has a significant timeframe*elev_m effect
#PC9 has sig timeframe*lat and timeframe*elev*lat effects 

lmer_results_grwssn %>% select(-data, -glm) %>% unnest(anova) %>% filter(PC=="PC1" | PC=="PC2" |PC=="PC3" | PC=="PC4") 
## # A tibble: 32 × 8
## # Groups:   PC [4]
##    PC    term      df deviance df.residual residual.deviance statistic   p.value
##    <chr> <chr>  <int>    <dbl>       <int>             <dbl>     <dbl>     <dbl>
##  1 PC1   NULL      NA NA                45             192.   NA       NA       
##  2 PC1   timef…     1  3.68e-1          44             192.    0.691    4.11e- 1
##  3 PC1   elev_m     1  1.70e+2          43              21.2 320.       4.22e-20
##  4 PC1   Lat        1  9.51e-1          42              20.3   1.79     1.89e- 1
##  5 PC1   timef…     1  4.95e-2          41              20.2   0.0930   7.62e- 1
##  6 PC1   timef…     1  2.90e-3          40              20.2   0.00545  9.42e- 1
##  7 PC1   elev_…     1  9.51e-4          39              20.2   0.00179  9.67e- 1
##  8 PC1   timef…     1  1.34e-2          38              20.2   0.0251   8.75e- 1
##  9 PC2   NULL      NA NA                45             132.   NA       NA       
## 10 PC2   timef…     1  9.76e+0          44             123.    5.21     2.82e- 2
## # ℹ 22 more rows

Plot climate change PCA

#prep the pc for making a plot with arrows distinguishing recent vs. historical time 
grwssn_avgs_locs.pc_avg <- grwssn_avgs_locs.pc %>%
  group_by(parent.pop, elev_m, timeframe) %>%
  summarise(across(.cols=starts_with("PC", ignore.case = FALSE), .fns = mean)) %>%
  ungroup()
## `summarise()` has grouped output by 'parent.pop', 'elev_m'. You can override
## using the `.groups` argument.
autoplot(grwssn_avgs.pc, data = grwssn_avgs,
         x=1, y=2,
         colour='elev_m', alpha=0.5,
         label=TRUE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

autoplot(grwssn_avgs.pc, data = grwssn_avgs,
         x=2, y=4,
         colour='elev_m', alpha=0.5,
         label=TRUE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

grwssn_avgs_locs.pc_avg %>% 
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC1, y=PC2, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_point(size=2, alpha=0.7) +
  geom_text_repel(aes(label = parent.pop)) +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed")  +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8)

grwssn_avgs_locs.pc_avg %>% 
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC2, y=PC4, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_point(size=2, alpha=0.7) +
  geom_text_repel(aes(label = parent.pop)) +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed")  +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8)

#grwssn_avgs_locs.pc_avg %>% filter(parent.pop=="WL2")
#grwssn_avgs_loadings
#PC1 = neg ann_tmean, tmean_wettest_month, ann_ppt
#PC2 = positive temp_seasonality and temp_ann_range, negative ppt
#PC4 = negative mean_diurnal_range
home_sites_pca_grwssn <- grwssn_avgs_locs.pc_avg %>%  
  mutate(group=str_c(parent.pop,elev_m))  %>%
  ggplot(aes(x=PC1, y=PC2, shape=timeframe, color=elev_m)) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  labs(x="PC1 (38.76%)", y="PC2 (26.73%)", color="Elevation (m)") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  annotate("text", x = 0.73, y= -0.15, label = "WL2", colour = "purple", size=7) +
  geom_path(aes(group=group),arrow = arrow(length=unit(5, "points")), linewidth = .8) +
  annotate("text", x = -4, y = -5.2, label = "Warm \n Wet", size=6) +
  annotate("text", x = 3.5, y = -5.2, label = "Cold \n Dry", size=6) +
  annotate("text", x = -5.4, y = -3, label = "Low Temp \n Seasonality", size=6) +
  annotate("text", x = -5.4, y = 2.7, label = "High Temp \n Seasonality", size=6) +
  coord_cartesian(ylim = c(-4, 3), xlim = c(-4.5,4), clip = "off") +
  theme_classic() +
  theme(text=element_text(size=28))

## add WL2 garden 2023 and 2024
WL2GRDN_grwssn_pc_prep_2023 <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2023, Season=="Growth Season") %>% 
  select(cwd:ppt, ann_tmean, mean_diurnal_range, temp_seasonality, temp_ann_range, tmean_wettest_month, ann_ppt, ppt_seasonality, ppt_coldest_month) 
WL2GRDN_grwssn_predicted_2023 <- predict(grwssn_avgs.pc, newdata = WL2GRDN_grwssn_pc_prep_2023)

WL2GRDN_grwssn_pc_prep_2024 <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2024, Season=="Growth Season") %>% 
  select(cwd:ppt, ann_tmean, mean_diurnal_range, temp_seasonality, temp_ann_range, tmean_wettest_month, ann_ppt, ppt_seasonality, ppt_coldest_month) 
WL2GRDN_grwssn_2024_predicted <- predict(grwssn_avgs.pc, newdata = WL2GRDN_grwssn_pc_prep_2024)

home_sites_pca_grwssn$data <- rbind(home_sites_pca_grwssn$data, 
  data.frame(
    parent.pop = "WL2_Garden",
    elev_m = 2020,
    timeframe = c("2023", "2024"),
    PC1 = c(WL2GRDN_grwssn_predicted_2023[, "PC1"], WL2GRDN_grwssn_2024_predicted[, "PC1"]),
    PC2 = c(WL2GRDN_grwssn_predicted_2023[, "PC2"], WL2GRDN_grwssn_2024_predicted[, "PC2"]),
    PC3 = c(WL2GRDN_grwssn_predicted_2023[, "PC3"], WL2GRDN_grwssn_2024_predicted[, "PC3"]),
    PC4 = c(WL2GRDN_grwssn_predicted_2023[, "PC4"], WL2GRDN_grwssn_2024_predicted[, "PC4"]),
    PC5 = c(WL2GRDN_grwssn_predicted_2023[, "PC5"], WL2GRDN_grwssn_2024_predicted[, "PC5"]),
    PC6 = c(WL2GRDN_grwssn_predicted_2023[, "PC6"], WL2GRDN_grwssn_2024_predicted[, "PC6"]),
    PC7 = c(WL2GRDN_grwssn_predicted_2023[, "PC7"], WL2GRDN_grwssn_2024_predicted[, "PC7"]),
    PC8 = c(WL2GRDN_grwssn_predicted_2023[, "PC8"], WL2GRDN_grwssn_2024_predicted[, "PC8"]),
    PC9 = c(WL2GRDN_grwssn_predicted_2023[, "PC9"], WL2GRDN_grwssn_2024_predicted[, "PC9"]),
    PC10 = c(WL2GRDN_grwssn_predicted_2023[, "PC10"], WL2GRDN_grwssn_2024_predicted[, "PC10"]),
    PC11 = c(WL2GRDN_grwssn_predicted_2023[, "PC11"], WL2GRDN_grwssn_2024_predicted[, "PC11"]),
    group = c("new", "new2")
  )
)

home_sites_pca_grwssn + 
  geom_point(data=filter(home_sites_pca_grwssn$data, parent.pop == "WL2_Garden"), size=6, shape = 18, show.legend = FALSE) +
  annotate("text", x = -0.04, y= -1.5, label = "WL2 Garden \n 2023", colour = "purple", size=6) +
  annotate("text", x = 1.5, y= 3, label = "WL2 Garden 2024", colour = "purple", size=6) 

ggsave("../Figures/Growth_Season_PC1-PC2_PlusGarden.png", width = 7.4, height = 6, units = "in")

WL2 Yearly Variation PCAs (Figure S2)

Water Year

wl2_wtryr_var <- all_clim %>% 
  filter(parent.pop=="WL2", Season=="Water Year") %>% 
  ungroup()

wl2_wtryr_var_normalized <- wl2_wtryr_var %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_wtr_yr_var = cor(wl2_wtryr_var_normalized) #test correlations among the traits
cor.sig_wtr_yr_var <- cor.mtest(wl2_wtryr_var_normalized, method = "pearson") #get pearson's test p-values 
cor.norm_wtr_yr_var
##                             cwd          pck           ppt         tmn
## cwd                  1.00000000 -0.706327469 -6.883437e-01  0.73251141
## pck                 -0.70632747  1.000000000  8.704319e-01 -0.40438843
## ppt                 -0.68834373  0.870431902  1.000000e+00 -0.28963515
## tmn                  0.73251141 -0.404388428 -2.896352e-01  1.00000000
## tmx                  0.85251859 -0.734923879 -7.035544e-01  0.75351699
## ann_tmean            0.84193071 -0.596630788 -5.157099e-01  0.94469376
## mean_diurnal_range   0.02264391 -0.358993431 -4.894150e-01 -0.50703158
## temp_seasonality     0.22169251  0.158401745  7.218531e-05  0.09443653
## temp_ann_range       0.13754077 -0.074078475 -2.262801e-01 -0.20779229
## ann_ppt             -0.68834373  0.870431902  1.000000e+00 -0.28963515
## ppt_seasonality      0.19403510  0.129979137  9.366670e-03  0.33264885
## tmean_wettest_month  0.31020889 -0.215672022 -2.351844e-02  0.27958068
## tmean_driest_month  -0.02876951  0.150749573  2.368924e-01  0.13034888
## ppt_warmest_month   -0.14386285 -0.002770736  3.042050e-03 -0.17448622
## ppt_coldest_month   -0.38920806  0.585320238  5.635365e-01 -0.03745338
##                              tmx   ann_tmean mean_diurnal_range
## cwd                  0.852518591  0.84193071         0.02264391
## pck                 -0.734923879 -0.59663079        -0.35899343
## ppt                 -0.703554351 -0.51570986        -0.48941503
## tmn                  0.753516995  0.94469376        -0.50703158
## tmx                  1.000000000  0.92744896         0.18459872
## ann_tmean            0.927448958  1.00000000        -0.19631717
## mean_diurnal_range   0.184598716 -0.19631717         1.00000000
## temp_seasonality     0.005302744  0.05636138        -0.13422443
## temp_ann_range       0.041359479 -0.09756177         0.36486107
## ann_ppt             -0.703554351 -0.51570986        -0.48941503
## ppt_seasonality      0.173718625  0.27587132        -0.26953366
## tmean_wettest_month  0.204520410  0.26105104        -0.14981712
## tmean_driest_month  -0.055787614  0.04631405        -0.26800419
## ppt_warmest_month   -0.139777643 -0.16897610         0.07758914
## ppt_coldest_month   -0.324111158 -0.18298444        -0.36893842
##                     temp_seasonality temp_ann_range       ann_ppt
## cwd                     2.216925e-01     0.13754077 -6.883437e-01
## pck                     1.584017e-01    -0.07407847  8.704319e-01
## ppt                     7.218531e-05    -0.22628008  1.000000e+00
## tmn                     9.443653e-02    -0.20779229 -2.896352e-01
## tmx                     5.302744e-03     0.04135948 -7.035544e-01
## ann_tmean               5.636138e-02    -0.09756177 -5.157099e-01
## mean_diurnal_range     -1.342244e-01     0.36486107 -4.894150e-01
## temp_seasonality        1.000000e+00     0.64375877  7.218531e-05
## temp_ann_range          6.437588e-01     1.00000000 -2.262801e-01
## ann_ppt                 7.218531e-05    -0.22628008  1.000000e+00
## ppt_seasonality         1.964532e-01    -0.03577943  9.366670e-03
## tmean_wettest_month    -1.132325e-01    -0.19491722 -2.351844e-02
## tmean_driest_month      9.012687e-02    -0.08375316  2.368924e-01
## ppt_warmest_month      -2.029536e-01    -0.20140285  3.042050e-03
## ppt_coldest_month       4.718246e-02    -0.13343442  5.635365e-01
##                     ppt_seasonality tmean_wettest_month tmean_driest_month
## cwd                      0.19403510         0.310208894       -0.028769508
## pck                      0.12997914        -0.215672022        0.150749573
## ppt                      0.00936667        -0.023518443        0.236892412
## tmn                      0.33264885         0.279580677        0.130348878
## tmx                      0.17371863         0.204520410       -0.055787614
## ann_tmean                0.27587132         0.261051042        0.046314049
## mean_diurnal_range      -0.26953366        -0.149817123       -0.268004194
## temp_seasonality         0.19645324        -0.113232508        0.090126866
## temp_ann_range          -0.03577943        -0.194917218       -0.083753164
## ann_ppt                  0.00936667        -0.023518443        0.236892412
## ppt_seasonality          1.00000000        -0.142813794        0.064433559
## tmean_wettest_month     -0.14281379         1.000000000       -0.005202109
## tmean_driest_month       0.06443356        -0.005202109        1.000000000
## ppt_warmest_month       -0.11901333        -0.055166562       -0.373682263
## ppt_coldest_month        0.46926251        -0.344307774        0.330952983
##                     ppt_warmest_month ppt_coldest_month
## cwd                      -0.143862853       -0.38920806
## pck                      -0.002770736        0.58532024
## ppt                       0.003042050        0.56353650
## tmn                      -0.174486220       -0.03745338
## tmx                      -0.139777643       -0.32411116
## ann_tmean                -0.168976099       -0.18298444
## mean_diurnal_range        0.077589137       -0.36893842
## temp_seasonality         -0.202953562        0.04718246
## temp_ann_range           -0.201402850       -0.13343442
## ann_ppt                   0.003042050        0.56353650
## ppt_seasonality          -0.119013335        0.46926251
## tmean_wettest_month      -0.055166562       -0.34430777
## tmean_driest_month       -0.373682263        0.33095298
## ppt_warmest_month         1.000000000       -0.14455760
## ppt_coldest_month        -0.144557597        1.00000000
cor.sig_wtr_yr_var$p
##                              cwd          pck          ppt          tmn
## cwd                 0.000000e+00 2.885331e-10 1.218447e-09 2.890355e-11
## pck                 2.885331e-10 0.000000e+00 1.679162e-19 1.352708e-03
## ppt                 1.218447e-09 1.679162e-19 0.000000e+00 2.478994e-02
## tmn                 2.890355e-11 1.352708e-03 2.478994e-02 0.000000e+00
## tmx                 5.539429e-18 2.306887e-11 3.628135e-10 3.724963e-12
## ann_tmean           3.545315e-17 4.883164e-07 2.475966e-05 9.106050e-30
## mean_diurnal_range  8.636488e-01 4.850261e-03 7.236193e-05 3.562142e-05
## temp_seasonality    8.868907e-02 2.267346e-01 9.995633e-01 4.729295e-01
## temp_ann_range      2.946546e-01 5.737669e-01 8.212016e-02 1.111295e-01
## ann_ppt             1.218447e-09 1.679162e-19 0.000000e+00 2.478994e-02
## ppt_seasonality     1.374031e-01 3.222514e-01 9.433745e-01 9.407024e-03
## tmean_wettest_month 1.585856e-02 9.792730e-02 8.584359e-01 3.050963e-02
## tmean_driest_month  8.272694e-01 2.502583e-01 6.839492e-02 3.208650e-01
## ppt_warmest_month   2.728024e-01 9.832372e-01 9.815960e-01 1.824046e-01
## ppt_coldest_month   2.115269e-03 9.017240e-07 2.758413e-06 7.763265e-01
##                              tmx    ann_tmean mean_diurnal_range
## cwd                 5.539429e-18 3.545315e-17       8.636488e-01
## pck                 2.306887e-11 4.883164e-07       4.850261e-03
## ppt                 3.628135e-10 2.475966e-05       7.236193e-05
## tmn                 3.724963e-12 9.106050e-30       3.562142e-05
## tmx                 0.000000e+00 1.875274e-26       1.579547e-01
## ann_tmean           1.875274e-26 0.000000e+00       1.327490e-01
## mean_diurnal_range  1.579547e-01 1.327490e-01       0.000000e+00
## temp_seasonality    9.679248e-01 6.688478e-01       3.065619e-01
## temp_ann_range      7.537002e-01 4.583389e-01       4.153303e-03
## ann_ppt             3.628135e-10 2.475966e-05       7.236193e-05
## ppt_seasonality     1.843663e-01 3.288126e-02       3.728833e-02
## tmean_wettest_month 1.169973e-01 4.394269e-02       2.532342e-01
## tmean_driest_month  6.720263e-01 7.252964e-01       3.842222e-02
## ppt_warmest_month   2.867959e-01 1.968239e-01       5.556939e-01
## ppt_coldest_month   1.152571e-02 1.616864e-01       3.722616e-03
##                     temp_seasonality temp_ann_range      ann_ppt
## cwd                     8.868907e-02   2.946546e-01 1.218447e-09
## pck                     2.267346e-01   5.737669e-01 1.679162e-19
## ppt                     9.995633e-01   8.212016e-02 0.000000e+00
## tmn                     4.729295e-01   1.111295e-01 2.478994e-02
## tmx                     9.679248e-01   7.537002e-01 3.628135e-10
## ann_tmean               6.688478e-01   4.583389e-01 2.475966e-05
## mean_diurnal_range      3.065619e-01   4.153303e-03 7.236193e-05
## temp_seasonality        0.000000e+00   2.882793e-08 9.995633e-01
## temp_ann_range          2.882793e-08   0.000000e+00 8.212016e-02
## ann_ppt                 9.995633e-01   8.212016e-02 0.000000e+00
## ppt_seasonality         1.324753e-01   7.860810e-01 9.433745e-01
## tmean_wettest_month     3.890069e-01   1.355898e-01 8.584359e-01
## tmean_driest_month      4.934514e-01   5.246321e-01 6.839492e-02
## ppt_warmest_month       1.198899e-01   1.228060e-01 9.815960e-01
## ppt_coldest_month       7.203544e-01   3.094436e-01 2.758413e-06
##                     ppt_seasonality tmean_wettest_month tmean_driest_month
## cwd                    0.1374031081          0.01585856        0.827269432
## pck                    0.3222514256          0.09792730        0.250258317
## ppt                    0.9433745208          0.85843594        0.068394915
## tmn                    0.0094070237          0.03050963        0.320865032
## tmx                    0.1843662586          0.11699733        0.672026273
## ann_tmean              0.0328812606          0.04394269        0.725296438
## mean_diurnal_range     0.0372883301          0.25323422        0.038422217
## temp_seasonality       0.1324753316          0.38900685        0.493451368
## temp_ann_range         0.7860809853          0.13558979        0.524632105
## ann_ppt                0.9433745208          0.85843594        0.068394915
## ppt_seasonality        0.0000000000          0.27635166        0.624763897
## tmean_wettest_month    0.2763516644          0.00000000        0.968533247
## tmean_driest_month     0.6247638970          0.96853325        0.000000000
## ppt_warmest_month      0.3650917462          0.67547316        0.003271663
## ppt_coldest_month      0.0001554574          0.00706429        0.009798638
##                     ppt_warmest_month ppt_coldest_month
## cwd                       0.272802412      2.115269e-03
## pck                       0.983237212      9.017240e-07
## ppt                       0.981596049      2.758413e-06
## tmn                       0.182404636      7.763265e-01
## tmx                       0.286795919      1.152571e-02
## ann_tmean                 0.196823933      1.616864e-01
## mean_diurnal_range        0.555693874      3.722616e-03
## temp_seasonality          0.119889877      7.203544e-01
## temp_ann_range            0.122806025      3.094436e-01
## ann_ppt                   0.981596049      2.758413e-06
## ppt_seasonality           0.365091746      1.554574e-04
## tmean_wettest_month       0.675473155      7.064290e-03
## tmean_driest_month        0.003271663      9.798638e-03
## ppt_warmest_month         0.000000000      2.704687e-01
## ppt_coldest_month         0.270468691      0.000000e+00
#ann_ppt and ppt 100% correlated (ppt = avg across monts, ann_ppt = avg of the total ppt in a year) - only keep ann_ppt 
#ann_tmean highly corr with tmn, tmx - take it out 

wl2_wtryr_var.pc = prcomp(wl2_wtryr_var[c(8:9, 11:12, 14:22)], scale = TRUE, center = TRUE)
summary(wl2_wtryr_var.pc)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6     PC7
## Standard deviation     2.0414 1.5870 1.3858 1.11291 1.03643 0.80685 0.70966
## Proportion of Variance 0.3206 0.1937 0.1477 0.09527 0.08263 0.05008 0.03874
## Cumulative Proportion  0.3206 0.5143 0.6620 0.75728 0.83991 0.88998 0.92872
##                            PC8    PC9    PC10    PC11    PC12      PC13
## Standard deviation     0.57599 0.4864 0.42556 0.31916 0.27436 2.322e-15
## Proportion of Variance 0.02552 0.0182 0.01393 0.00784 0.00579 0.000e+00
## Cumulative Proportion  0.95424 0.9724 0.98637 0.99421 1.00000 1.000e+00
tibble(PC=str_c("PC",str_pad(1:13,2,pad="0")),
       percent_var=wl2_wtryr_var.pc$sdev[1:13]^2/sum(wl2_wtryr_var.pc$sdev^2)*100) %>%
  ggplot(aes(x=PC, y=percent_var)) +
  geom_col() +
  ggtitle("Percent Variance Explained")

#combine pcs with metadata
wl2_wtryr_var.pc.dat = data.frame(wl2_wtryr_var.pc$x)
wl2_wtryr_var_locs.pc = cbind(wl2_wtryr_var, wl2_wtryr_var.pc.dat) %>% select(parent.pop:year, PC1:PC13)
wl2_wtryr_var_loadings = data.frame(varnames=rownames(wl2_wtryr_var.pc$rotation), wl2_wtryr_var.pc$rotation)
wl2_wtryr_var_loadings
##                                varnames          PC1         PC2         PC3
## cwd                                 cwd  0.436983367  0.20245646  0.05914300
## pck                                 pck -0.442699899  0.10155366  0.07303354
## tmn                                 tmn  0.288600920  0.45859941 -0.13529934
## tmx                                 tmx  0.438031973  0.16160038 -0.01005479
## mean_diurnal_range   mean_diurnal_range  0.142845627 -0.47370951  0.18908155
## temp_seasonality       temp_seasonality  0.016311978  0.15487758  0.56775231
## temp_ann_range           temp_ann_range  0.074890388 -0.14682469  0.62908917
## ann_ppt                         ann_ppt -0.430324868  0.14711055 -0.09536193
## ppt_seasonality         ppt_seasonality -0.003010692  0.38643033  0.15005153
## tmean_wettest_month tmean_wettest_month  0.152030759  0.08381173 -0.31507960
## tmean_driest_month   tmean_driest_month -0.094759098  0.31183613  0.06767535
## ppt_warmest_month     ppt_warmest_month -0.034841714 -0.25272138 -0.27255555
## ppt_coldest_month     ppt_coldest_month -0.305164173  0.32127976  0.09572096
##                               PC4         PC5          PC6         PC7
## cwd                  0.0005698593 -0.09868912 -0.049149321 -0.08162993
## pck                  0.0191743564 -0.17206314  0.110985163  0.01478149
## tmn                 -0.0472323390 -0.12245316 -0.158743137  0.22248967
## tmx                 -0.1216647961  0.11849681  0.004727255  0.01109850
## mean_diurnal_range  -0.0889003661  0.33841605  0.243508674 -0.31805723
## temp_seasonality     0.1344517052 -0.43284986 -0.185030500 -0.06818229
## temp_ann_range       0.1218037763 -0.15798484  0.024608336 -0.07066957
## ann_ppt              0.1624867380 -0.15794168  0.055176339  0.01477315
## ppt_seasonality     -0.5148110180 -0.01753416  0.422852603 -0.35287736
## tmean_wettest_month  0.5116719546 -0.31358765  0.477293067 -0.45032544
## tmean_driest_month   0.3721497039  0.49347545 -0.462926043 -0.49981111
## ppt_warmest_month   -0.4081150706 -0.43399911 -0.483547673 -0.49272535
## ppt_coldest_month   -0.2889433863  0.22206156  0.090168063 -0.11210139
##                             PC8         PC9        PC10        PC11        PC12
## cwd                  0.05086382 -0.10530223 -0.13174931 -0.83433245 -0.13636662
## pck                  0.13209122 -0.51166806  0.14736785 -0.24189888  0.62159113
## tmn                  0.21071408 -0.03435290  0.19555705  0.22403851  0.10489693
## tmx                  0.43275937 -0.33184786  0.14971623  0.29136671  0.03496071
## mean_diurnal_range   0.25236885 -0.38371671 -0.09605862  0.04707553 -0.11097874
## temp_seasonality    -0.10209115 -0.25165977 -0.49018475  0.27945672 -0.13019373
## temp_ann_range       0.24991099  0.42215057  0.52660396 -0.06472166  0.09756226
## ann_ppt              0.30549620 -0.21461309  0.28154629 -0.06476127 -0.71177656
## ppt_seasonality     -0.42758973 -0.04022233  0.24009674  0.07318879 -0.10967507
## tmean_wettest_month  0.11497724  0.15799708 -0.10835357  0.11392014  0.13014943
## tmean_driest_month  -0.13608741 -0.05400561  0.13644929  0.01734213  0.05213777
## ppt_warmest_month    0.14305222  0.04590312  0.07136211  0.01475864  0.03521739
## ppt_coldest_month    0.53230738  0.38646922 -0.44842060 -0.05233012  0.06682770
##                              PC13
## cwd                 -1.801745e-16
## pck                 -7.060016e-16
## tmn                  6.716723e-01
## tmx                 -5.890564e-01
## mean_diurnal_range   4.492981e-01
## temp_seasonality     3.817788e-16
## temp_ann_range      -4.782631e-17
## ann_ppt              3.790174e-16
## ppt_seasonality     -2.887139e-16
## tmean_wettest_month  1.684982e-16
## tmean_driest_month   1.225983e-16
## ppt_warmest_month   -5.150502e-17
## ppt_coldest_month    5.087270e-16

Plot the PCA

autoplot(wl2_wtryr_var.pc, data = wl2_wtryr_var,
         colour='year', alpha=0.6,
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_color_viridis() +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

wl2_yrl_var_pca_wtryr <- wl2_wtryr_var_locs.pc %>% 
  mutate(group=str_c(year))  %>%
  ggplot(aes(x=PC1, y=PC2, color=year)) +
  geom_point(size=4, alpha=0.7) +
  scale_color_viridis() +
  labs(x="PC1 (32.06%)", y="PC2 (19.37%)", color="Year") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  #PC1: cwd and tmx = pos; pck and ann_ppt = neg:
  annotate("text", x = -5, y = -6.4, label = "Wet \n Snow", size=6) + 
  annotate("text", x = 4, y = -6.4, label = "Dry \n No Snow", size=6) +
  #PC2: tmn = pos; mean diurnal range = neg:
  annotate("text", x = -7.8, y = -3.7, label = "Low Min \n Temp", size=6) +
  annotate("text", x = -7.8, y = 4, label = "High Min \n Temp", size=6) +
  coord_cartesian(ylim = c(-5, 5), xlim = c(-6,5), clip = "off") +
  theme_classic() +
  theme(text=element_text(size=28))

## add WL2 garden 2023 and 2024
WL2GRDN_wtryr_pc_prep_2023_var <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2023, Season=="Water Year") %>% 
  select(cwd:pck, tmn:tmx, mean_diurnal_range, temp_seasonality, temp_ann_range, ann_ppt, ppt_seasonality, tmean_wettest_month, tmean_driest_month, ppt_warmest_month, ppt_coldest_month) 
WL2GRDN_wtryr_predicted_2023_var <- predict(wl2_wtryr_var.pc, newdata = WL2GRDN_wtryr_pc_prep_2023_var)

WL2GRDN_wtryr_pc_prep_2024_var <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2024, Season=="Water Year") %>% 
  select(cwd:pck, tmn:tmx, mean_diurnal_range, temp_seasonality, temp_ann_range, ann_ppt, ppt_seasonality, tmean_wettest_month, tmean_driest_month, ppt_warmest_month, ppt_coldest_month) 
WL2GRDN_wtryr_2024_var_predicted <- predict(wl2_wtryr_var.pc, newdata = WL2GRDN_wtryr_pc_prep_2024_var)

wl2_yrl_var_pca_wtryr$data <- rbind(wl2_yrl_var_pca_wtryr$data, 
  data.frame(
    parent.pop = "WL2_Garden",
    elevation.group="High",
    elev_m = 2020,
    Lat = 38.8263,
    Long=-120.2524,
    timeframe = c("2023", "2024"),
    year = c(2023, 2024),
    PC1 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC1"], WL2GRDN_wtryr_2024_var_predicted[, "PC1"]),
    PC2 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC2"], WL2GRDN_wtryr_2024_var_predicted[, "PC2"]),
    PC3 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC3"], WL2GRDN_wtryr_2024_var_predicted[, "PC3"]),
    PC4 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC4"], WL2GRDN_wtryr_2024_var_predicted[, "PC4"]),
    PC5 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC5"], WL2GRDN_wtryr_2024_var_predicted[, "PC5"]),
    PC6 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC6"], WL2GRDN_wtryr_2024_var_predicted[, "PC6"]),
    PC7 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC7"], WL2GRDN_wtryr_2024_var_predicted[, "PC7"]),
    PC8 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC8"], WL2GRDN_wtryr_2024_var_predicted[, "PC8"]),
    PC9 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC9"], WL2GRDN_wtryr_2024_var_predicted[, "PC9"]),
    PC10 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC10"], WL2GRDN_wtryr_2024_var_predicted[, "PC10"]),
    PC11 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC11"], WL2GRDN_wtryr_2024_var_predicted[, "PC11"]),
    PC12 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC12"], WL2GRDN_wtryr_2024_var_predicted[, "PC12"]),
    PC13 = c(WL2GRDN_wtryr_predicted_2023_var[, "PC13"], WL2GRDN_wtryr_2024_var_predicted[, "PC13"]),
    group = c("new", "new2")
  )
)

wl2_yrl_var_pca_wtryr + 
  geom_point(data=filter(wl2_yrl_var_pca_wtryr$data, parent.pop == "WL2_Garden"), size=8, shape = 18, show.legend = FALSE) +
  annotate("text", x = -5.5, y= 1.2, label = "WL2 \n Garden \n 2023", colour = "purple", size=7) +
  annotate("text", x = 0.9, y= 3, label = "WL2 Garden 2024", colour = "purple", size=7) 

ggsave("../Figures/Wtr_Year_WL2ONLY_PC1-PC2.png", width = 7.4, height = 6, units = "in")

Growth Season

wl2_grwssn_var <- all_clim %>% 
  filter(parent.pop=="WL2", Season=="Growth Season") %>% 
  ungroup()

wl2_grwssn_var_normalized <- wl2_grwssn_var %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_grwssn_var = cor(wl2_grwssn_var_normalized) #test correlations among the traits
cor.sig_grwssn_var <- cor.mtest(wl2_grwssn_var_normalized, method = "pearson") #get pearson's test p-values 
cor.norm_grwssn_var
##                              cwd         pck          ppt         tmn
## cwd                  1.000000000  0.07108600  0.001915216  0.79236684
## pck                  0.071085998  1.00000000  0.721708604  0.00673063
## ppt                  0.001915216  0.72170860  1.000000000  0.01524654
## tmn                  0.792366844  0.00673063  0.015246540  1.00000000
## tmx                  0.875849564 -0.04825579 -0.121627851  0.74920992
## ann_tmean            0.891981908 -0.02225487 -0.057009796  0.93495785
## mean_diurnal_range   0.122180554 -0.07774622 -0.193536791 -0.34959280
## temp_seasonality     0.032901868  0.29582015 -0.048850171  0.15848266
## temp_ann_range       0.191352846  0.16612463 -0.290531412  0.06602322
## ann_ppt             -0.073070789  0.64979578  0.835647084 -0.03275025
## ppt_seasonality      0.433765839  0.46979081  0.265838642  0.34873776
## tmean_wettest_month -0.032567737 -0.49166563 -0.089105470  0.01633325
## tmean_driest_month   0.142440419  0.17918168  0.166686105  0.31931077
## ppt_warmest_month   -0.156830607  0.07632095  0.149144244 -0.05579174
## ppt_coldest_month    0.232549236  0.83036687  0.828691127  0.16885690
##                             tmx    ann_tmean mean_diurnal_range
## cwd                  0.87584956  0.891981908        0.122180554
## pck                 -0.04825579 -0.022254875       -0.077746222
## ppt                 -0.12162785 -0.057009796       -0.193536791
## tmn                  0.74920992  0.934957847       -0.349592800
## tmx                  1.00000000  0.935448038        0.358622183
## ann_tmean            0.93544804  1.000000000        0.005519632
## mean_diurnal_range   0.35862218  0.005519632        1.000000000
## temp_seasonality    -0.01307287  0.077574674       -0.241855580
## temp_ann_range       0.13596388  0.108059352        0.099275288
## ann_ppt             -0.26674903 -0.160353895       -0.331171735
## ppt_seasonality      0.47350006  0.439725671        0.178282515
## tmean_wettest_month -0.01029730  0.003201056       -0.037585894
## tmean_driest_month   0.16713505  0.259926162       -0.213611695
## ppt_warmest_month   -0.22683918 -0.151273824       -0.242242939
## ppt_coldest_month    0.15560595  0.173458861       -0.017872475
##                     temp_seasonality temp_ann_range     ann_ppt ppt_seasonality
## cwd                       0.03290187    0.191352846 -0.07307079     0.433765839
## pck                       0.29582015    0.166124631  0.64979578     0.469790812
## ppt                      -0.04885017   -0.290531412  0.83564708     0.265838642
## tmn                       0.15848266    0.066023216 -0.03275025     0.348737764
## tmx                      -0.01307287    0.135963882 -0.26674903     0.473500056
## ann_tmean                 0.07757467    0.108059352 -0.16035389     0.439725671
## mean_diurnal_range       -0.24185558    0.099275288 -0.33117173     0.178282515
## temp_seasonality          1.00000000    0.675079272  0.05231027     0.052749436
## temp_ann_range            0.67507927    1.000000000 -0.01620131    -0.001332629
## ann_ppt                   0.05231027   -0.016201313  1.00000000     0.161195707
## ppt_seasonality           0.05274944   -0.001332629  0.16119571     1.000000000
## tmean_wettest_month      -0.19373204   -0.294347252 -0.10723315    -0.206824039
## tmean_driest_month        0.41650884    0.154921582  0.19077217     0.237659643
## ppt_warmest_month        -0.24057049   -0.309529796  0.06344700    -0.332376918
## ppt_coldest_month        -0.02718380   -0.163858464  0.66991108     0.603064543
##                     tmean_wettest_month tmean_driest_month ppt_warmest_month
## cwd                        -0.032567737          0.1424404       -0.15683061
## pck                        -0.491665632          0.1791817        0.07632095
## ppt                        -0.089105470          0.1666861        0.14914424
## tmn                         0.016333251          0.3193108       -0.05579174
## tmx                        -0.010297296          0.1671351       -0.22683918
## ann_tmean                   0.003201056          0.2599262       -0.15127382
## mean_diurnal_range         -0.037585894         -0.2136117       -0.24224294
## temp_seasonality           -0.193732039          0.4165088       -0.24057049
## temp_ann_range             -0.294347252          0.1549216       -0.30952980
## ann_ppt                    -0.107233145          0.1907722        0.06344700
## ppt_seasonality            -0.206824039          0.2376596       -0.33237692
## tmean_wettest_month         1.000000000         -0.1466569       -0.04669263
## tmean_driest_month         -0.146656859          1.0000000       -0.46679234
## ppt_warmest_month          -0.046692625         -0.4667923        1.00000000
## ppt_coldest_month          -0.323686290          0.2128168        0.04222958
##                     ppt_coldest_month
## cwd                        0.23254924
## pck                        0.83036687
## ppt                        0.82869113
## tmn                        0.16885690
## tmx                        0.15560595
## ann_tmean                  0.17345886
## mean_diurnal_range        -0.01787248
## temp_seasonality          -0.02718380
## temp_ann_range            -0.16385846
## ann_ppt                    0.66991108
## ppt_seasonality            0.60306454
## tmean_wettest_month       -0.32368629
## tmean_driest_month         0.21281684
## ppt_warmest_month          0.04222958
## ppt_coldest_month          1.00000000
cor.sig_grwssn_var$p
##                              cwd          pck          ppt          tmn
## cwd                 0.000000e+00 5.893830e-01 9.884126e-01 4.636813e-14
## pck                 5.893830e-01 0.000000e+00 7.707029e-11 9.592944e-01
## ppt                 9.884126e-01 7.707029e-11 0.000000e+00 9.079525e-01
## tmn                 4.636813e-14 9.592944e-01 9.079525e-01 0.000000e+00
## tmx                 5.260109e-20 7.142620e-01 3.545760e-01 5.763379e-12
## ann_tmean           1.169900e-21 8.659696e-01 6.652627e-01 8.762987e-28
## mean_diurnal_range  3.523771e-01 5.548916e-01 1.384355e-01 6.182483e-03
## temp_seasonality    8.029252e-01 2.174262e-02 7.108957e-01 2.264943e-01
## temp_ann_range      1.430288e-01 2.045971e-01 2.432722e-02 6.162283e-01
## ann_ppt             5.790041e-01 1.936250e-08 1.001585e-16 8.038153e-01
## ppt_seasonality     5.355162e-04 1.524636e-04 4.007636e-02 6.318244e-03
## tmean_wettest_month 8.048870e-01 6.623998e-05 4.983822e-01 9.014233e-01
## tmean_driest_month  2.776223e-01 1.707325e-01 2.030495e-01 1.288913e-02
## ppt_warmest_month   2.314344e-01 5.621912e-01 2.553966e-01 6.720034e-01
## ppt_coldest_month   7.377161e-02 2.318982e-16 3.008875e-16 1.971446e-01
##                              tmx    ann_tmean mean_diurnal_range
## cwd                 5.260109e-20 1.169900e-21        0.352377063
## pck                 7.142620e-01 8.659696e-01        0.554891565
## ppt                 3.545760e-01 6.652627e-01        0.138435475
## tmn                 5.763379e-12 8.762987e-28        0.006182483
## tmx                 0.000000e+00 7.085082e-28        0.004897635
## ann_tmean           7.085082e-28 0.000000e+00        0.966613661
## mean_diurnal_range  4.897635e-03 9.666137e-01        0.000000000
## temp_seasonality    9.210302e-01 5.557678e-01        0.062639708
## temp_ann_range      3.002783e-01 4.111754e-01        0.450444678
## ann_ppt             3.937394e-02 2.209878e-01        0.009747342
## ppt_seasonality     1.328874e-04 4.391553e-04        0.172924373
## tmean_wettest_month 9.377591e-01 9.806343e-01        0.775555714
## tmean_driest_month  2.018182e-01 4.489426e-02        0.101255224
## ppt_warmest_month   8.134658e-02 2.485957e-01        0.062207463
## ppt_coldest_month   2.351443e-01 1.850335e-01        0.892186399
##                     temp_seasonality temp_ann_range      ann_ppt
## cwd                     8.029252e-01   1.430288e-01 5.790041e-01
## pck                     2.174262e-02   2.045971e-01 1.936250e-08
## ppt                     7.108957e-01   2.432722e-02 1.001585e-16
## tmn                     2.264943e-01   6.162283e-01 8.038153e-01
## tmx                     9.210302e-01   3.002783e-01 3.937394e-02
## ann_tmean               5.557678e-01   4.111754e-01 2.209878e-01
## mean_diurnal_range      6.263971e-02   4.504447e-01 9.747342e-03
## temp_seasonality        0.000000e+00   3.306021e-09 6.914103e-01
## temp_ann_range          3.306021e-09   0.000000e+00 9.022156e-01
## ann_ppt                 6.914103e-01   9.022156e-01 0.000000e+00
## ppt_seasonality         6.889510e-01   9.919372e-01 2.185414e-01
## tmean_wettest_month     1.380303e-01   2.243790e-02 4.147824e-01
## tmean_driest_month      9.322849e-04   2.372352e-01 1.442690e-01
## ppt_warmest_month       6.409107e-02   1.610188e-02 6.300860e-01
## ppt_coldest_month       8.366555e-01   2.109279e-01 4.811186e-09
##                     ppt_seasonality tmean_wettest_month tmean_driest_month
## cwd                    5.355162e-04        8.048870e-01       0.2776222600
## pck                    1.524636e-04        6.623998e-05       0.1707324668
## ppt                    4.007636e-02        4.983822e-01       0.2030495458
## tmn                    6.318244e-03        9.014233e-01       0.0128891339
## tmx                    1.328874e-04        9.377591e-01       0.2018181597
## ann_tmean              4.391553e-04        9.806343e-01       0.0448942579
## mean_diurnal_range     1.729244e-01        7.755557e-01       0.1012552236
## temp_seasonality       6.889510e-01        1.380303e-01       0.0009322849
## temp_ann_range         9.919372e-01        2.243790e-02       0.2372352020
## ann_ppt                2.185414e-01        4.147824e-01       0.1442689879
## ppt_seasonality        0.000000e+00        1.128419e-01       0.0674785641
## tmean_wettest_month    1.128419e-01        0.000000e+00       0.2634982156
## tmean_driest_month     6.747856e-02        2.634982e-01       0.0000000000
## ppt_warmest_month      9.468890e-03        7.231406e-01       0.0001701831
## ppt_coldest_month      3.408547e-07        1.164114e-02       0.1025623095
##                     ppt_warmest_month ppt_coldest_month
## cwd                      0.2314344471      7.377161e-02
## pck                      0.5621912098      2.318982e-16
## ppt                      0.2553965582      3.008875e-16
## tmn                      0.6720034007      1.971446e-01
## tmx                      0.0813465764      2.351443e-01
## ann_tmean                0.2485956632      1.850335e-01
## mean_diurnal_range       0.0622074629      8.921864e-01
## temp_seasonality         0.0640910665      8.366555e-01
## temp_ann_range           0.0161018750      2.109279e-01
## ann_ppt                  0.6300860398      4.811186e-09
## ppt_seasonality          0.0094688895      3.408547e-07
## tmean_wettest_month      0.7231406341      1.164114e-02
## tmean_driest_month       0.0001701831      1.025623e-01
## ppt_warmest_month        0.0000000000      7.486872e-01
## ppt_coldest_month        0.7486872309      0.000000e+00
#ann_tmean highly corr with tmn, tmx - take it out 

wl2_grwssn_var.pc = prcomp(wl2_grwssn_var[c(8:12, 14:22)], scale = TRUE, center = TRUE)
summary(wl2_grwssn_var.pc)
## Importance of components:
##                           PC1    PC2    PC3    PC4     PC5     PC6     PC7
## Standard deviation     1.9682 1.7600 1.4430 1.2494 1.09753 0.87053 0.71059
## Proportion of Variance 0.2767 0.2213 0.1487 0.1115 0.08604 0.05413 0.03607
## Cumulative Proportion  0.2767 0.4980 0.6467 0.7582 0.84423 0.89836 0.93443
##                            PC8     PC9    PC10   PC11    PC12    PC13     PC14
## Standard deviation     0.64409 0.44283 0.33614 0.2994 0.24307 0.21307 1.13e-15
## Proportion of Variance 0.02963 0.01401 0.00807 0.0064 0.00422 0.00324 0.00e+00
## Cumulative Proportion  0.96406 0.97807 0.98614 0.9925 0.99676 1.00000 1.00e+00
tibble(PC=str_c("PC",str_pad(1:14,2,pad="0")),
       percent_var=wl2_grwssn_var.pc$sdev[1:14]^2/sum(wl2_grwssn_var.pc$sdev^2)*100) %>%
  ggplot(aes(x=PC, y=percent_var)) +
  geom_col() +
  ggtitle("Percent Variance Explained")

#combine pcs with metadata
wl2_grwssn_var.pc.dat = data.frame(wl2_grwssn_var.pc$x)
wl2_grwssn_var_locs.pc = cbind(wl2_grwssn_var, wl2_grwssn_var.pc.dat) %>% select(parent.pop:year, PC1:PC14)
wl2_grwssn_var_loadings = data.frame(varnames=rownames(wl2_grwssn_var.pc$rotation), wl2_grwssn_var.pc$rotation)
wl2_grwssn_var_loadings
##                                varnames         PC1         PC2         PC3
## cwd                                 cwd -0.21631334  0.42556298  0.21510451
## pck                                 pck -0.42681741 -0.18924520 -0.10813666
## ppt                                 ppt -0.37671792 -0.30361310  0.14685310
## tmn                                 tmn -0.21075853  0.37060449  0.14355308
## tmx                                 tmx -0.15925575  0.48124787  0.24391924
## mean_diurnal_range   mean_diurnal_range  0.07176527  0.15842351  0.14271383
## temp_seasonality       temp_seasonality -0.12899049  0.11551843 -0.56653923
## temp_ann_range           temp_ann_range -0.05274955  0.21186364 -0.51404283
## ann_ppt                         ann_ppt -0.33999384 -0.31354318 -0.03053116
## ppt_seasonality         ppt_seasonality -0.34227573  0.18228507  0.13267491
## tmean_wettest_month tmean_wettest_month  0.19872279  0.02424267  0.22338048
## tmean_driest_month   tmean_driest_month -0.22433906  0.13898586 -0.28492298
## ppt_warmest_month     ppt_warmest_month  0.06660781 -0.25772149  0.24108489
## ppt_coldest_month     ppt_coldest_month -0.45580953 -0.14409462  0.16448701
##                             PC4         PC5         PC6          PC7
## cwd                  0.09530532 -0.17609967 -0.19256391 -0.184970830
## pck                 -0.17008271 -0.18129930 -0.06311777  0.140463293
## ppt                  0.05924659  0.11868507 -0.18792222 -0.183028965
## tmn                  0.44842068 -0.14100082  0.06076465  0.018906473
## tmx                 -0.03906588 -0.08533311 -0.07354074 -0.166747223
## mean_diurnal_range  -0.68725874  0.07801684 -0.18966785 -0.262518602
## temp_seasonality     0.13137843 -0.09650536 -0.19202250  0.285401305
## temp_ann_range      -0.13910707 -0.25637354 -0.39937152 -0.107075070
## ann_ppt              0.12020723  0.11143419 -0.31719682 -0.258568808
## ppt_seasonality     -0.23279880  0.15315298  0.16387021  0.674595469
## tmean_wettest_month  0.28360856  0.44835251 -0.62924657  0.251900053
## tmean_driest_month   0.20642352  0.43882995  0.39767533 -0.369452651
## ppt_warmest_month    0.20133270 -0.61981577  0.01487830  0.003157498
## ppt_coldest_month   -0.12129024  0.01031220  0.01975454  0.025757065
##                             PC8         PC9        PC10        PC11
## cwd                 -0.12386660 -0.09500870 -0.39435451  0.65509296
## pck                  0.13542833 -0.11310706 -0.48087994 -0.20793000
## ppt                  0.14313920 -0.29426742  0.19208421  0.06824210
## tmn                 -0.08000572 -0.07718079  0.17497205 -0.34792443
## tmx                  0.16090219 -0.03041269  0.29774657 -0.31294634
## mean_diurnal_range   0.34036300  0.06575736  0.17457375  0.04768224
## temp_seasonality     0.44001514 -0.30781909  0.31560367  0.25788954
## temp_ann_range      -0.23266939  0.36351540 -0.13593986 -0.26277070
## ann_ppt             -0.35162459  0.30590509  0.39260665  0.16301237
## ppt_seasonality     -0.10835970  0.39158816  0.17568417  0.19149286
## tmean_wettest_month  0.26084834  0.16734672 -0.23567276 -0.12662979
## tmean_driest_month   0.36776044  0.38424004 -0.17400538  0.05538355
## ppt_warmest_month    0.45021759  0.47495498  0.05497489  0.10428023
## ppt_coldest_month    0.09259849 -0.07306279 -0.18618923 -0.27004835
##                              PC12         PC13          PC14
## cwd                  0.0733625621  0.005617524  2.343596e-16
## pck                 -0.5383363248  0.298977954 -7.121515e-17
## ppt                 -0.1905553459 -0.687734960  5.842040e-16
## tmn                 -0.0961644655  0.032514508 -6.310977e-01
## tmx                 -0.1248252557  0.106439481  6.334091e-01
## mean_diurnal_range  -0.0410384827  0.104738314 -4.477818e-01
## temp_seasonality     0.1492168464  0.165547109  2.193148e-16
## temp_ann_range       0.0929882919 -0.386777713  1.347314e-17
## ann_ppt             -0.0435459524  0.437031651  8.609880e-17
## ppt_seasonality     -0.0820347247 -0.181895532  9.322366e-17
## tmean_wettest_month -0.0003577508  0.039247525 -5.897955e-17
## tmean_driest_month  -0.0434393683 -0.035073449  8.528549e-17
## ppt_warmest_month    0.0261075475 -0.056806715  1.202675e-16
## ppt_coldest_month    0.7745194276  0.084626061 -3.678699e-16

Plot the PCA

autoplot(wl2_grwssn_var.pc, data = wl2_grwssn_var,
         colour='year', alpha=0.6,
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_color_viridis() +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

wl2_yrl_var_pca_grwssn <- wl2_grwssn_var_locs.pc %>% 
  mutate(group=str_c(year))  %>%
  ggplot(aes(x=PC1, y=PC2, color=year)) +
  geom_point(size=4, alpha=0.7) +
  scale_color_viridis() +
  labs(x="PC1 (27.67%)", y="PC2 (22.13%)", color="Year") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  #PC1: pck and ppt_coldest_month = neg:
  annotate("text", x = -4.5, y = -5.8, label = "Snow", size=6) + 
  annotate("text", x = 3.2, y = -5.8, label = "No Snow", size=6) +
  #PC2: cwd and tmx = pos:
  annotate("text", x = -6.6, y = -3.7, label = "Wet \n Cold", size=6) +
  annotate("text", x = -6.6, y = 3.5, label = "Dry \n Hot", size=6) +
  coord_cartesian(ylim = c(-4.5, 4.5), xlim = c(-5.5,3.5), clip = "off") +
  theme_classic() +
  theme(text=element_text(size=28))

## add WL2 garden 2023 and 2024
WL2GRDN_grwssn_pc_prep_2023_var <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2023, Season=="Growth Season") %>% 
  select(cwd:tmx, mean_diurnal_range, temp_seasonality, temp_ann_range, ann_ppt, ppt_seasonality, tmean_wettest_month, tmean_driest_month, ppt_warmest_month, ppt_coldest_month) 
WL2GRDN_grwssn_predicted_2023_var <- predict(wl2_grwssn_var.pc, newdata = WL2GRDN_grwssn_pc_prep_2023_var)

WL2GRDN_grwssn_pc_prep_2024_var <- all_clim %>% 
  ungroup() %>% 
  filter(parent.pop=="WL2_Garden", year==2024, Season=="Growth Season") %>% 
  select(cwd:tmx, mean_diurnal_range, temp_seasonality, temp_ann_range, ann_ppt, ppt_seasonality, tmean_wettest_month, tmean_driest_month, ppt_warmest_month, ppt_coldest_month) 
WL2GRDN_grwssn_2024_var_predicted <- predict(wl2_grwssn_var.pc, newdata = WL2GRDN_grwssn_pc_prep_2024_var)

wl2_yrl_var_pca_grwssn$data <- rbind(wl2_yrl_var_pca_grwssn$data, 
  data.frame(
    parent.pop = "WL2_Garden",
    elevation.group="High",
    elev_m = 2020,
    Lat = 38.8263,
    Long=-120.2524,
    timeframe = c("2023", "2024"),
    year = c(2023, 2024),
    PC1 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC1"], WL2GRDN_grwssn_2024_var_predicted[, "PC1"]),
    PC2 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC2"], WL2GRDN_grwssn_2024_var_predicted[, "PC2"]),
    PC3 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC3"], WL2GRDN_grwssn_2024_var_predicted[, "PC3"]),
    PC4 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC4"], WL2GRDN_grwssn_2024_var_predicted[, "PC4"]),
    PC5 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC5"], WL2GRDN_grwssn_2024_var_predicted[, "PC5"]),
    PC6 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC6"], WL2GRDN_grwssn_2024_var_predicted[, "PC6"]),
    PC7 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC7"], WL2GRDN_grwssn_2024_var_predicted[, "PC7"]),
    PC8 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC8"], WL2GRDN_grwssn_2024_var_predicted[, "PC8"]),
    PC9 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC9"], WL2GRDN_grwssn_2024_var_predicted[, "PC9"]),
    PC10 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC10"], WL2GRDN_grwssn_2024_var_predicted[, "PC10"]),
    PC11 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC11"], WL2GRDN_grwssn_2024_var_predicted[, "PC11"]),
    PC12 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC12"], WL2GRDN_grwssn_2024_var_predicted[, "PC12"]),
    PC13 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC13"], WL2GRDN_grwssn_2024_var_predicted[, "PC13"]),
    PC14 = c(WL2GRDN_grwssn_predicted_2023_var[, "PC14"], WL2GRDN_grwssn_2024_var_predicted[, "PC14"]),
    group = c("new", "new2")
  )
)

wl2_yrl_var_pca_grwssn + 
  geom_point(data=filter(wl2_yrl_var_pca_grwssn$data, parent.pop == "WL2_Garden"), size=8, shape = 18, show.legend = FALSE) +
  annotate("text", x = 1.19, y= -1.5, label = "WL2 Garden \n 2023", colour = "purple", size=7) +
  annotate("text", x = -0.51, y= 4.6, label = "WL2 Garden 2024", colour = "purple", size=7) 

ggsave("../Figures/Growth_Ssn_WL2ONLY_PC1-PC2.png", width = 7.4, height = 6, units = "in")

#WL2GRDN_grwssn_pc_prep_2023_var %>% select(pck)
#WL2GRDN_grwssn_pc_prep_2024_var %>% select(pck)
#wl2_grwssn_var %>% select(pck)

All climate PCA (all years, water year, growing season, historic and recent)

all_clim = all_clim %>%
  #remove data before historic (which should be just WL2_Garden)
  filter(year >= 1964) %>%
  #create time frames for WL2 garden site
  mutate(timeframe = if_else(parent.pop == "WL2_Garden" & year < 1994, "Historic", timeframe)) %>%
  mutate(timeframe = if_else(parent.pop == "WL2_Garden" & year >= 1994 & year != 2024, "recent", timeframe)) %>%
  mutate(timeframe = if_else(parent.pop == "WL2_Garden" & year == 2024, "2024Garden", timeframe))%>%
  mutate(timeframe = if_else(parent.pop == "WL2_Garden" & year == 2023, "2023Garden", timeframe)) %>%
  mutate(timeframe = as.factor(timeframe), Season = as.factor(Season), 
         parent.pop = as.factor(parent.pop), elevation.group = as.factor(elevation.group)) %>%
    #combine season and timeframe 
  mutate(seasontime = as.factor(paste(Season, timeframe, sep = "_"))) %>% 
  mutate(timeframe=if_else(timeframe=="recent", "Recent",
                 if_else(timeframe=="historic", "Historic", timeframe)))
#summary(all_clim)

allclim_normalized <- all_clim %>% 
  ungroup() %>% 
  select(cwd:ppt_coldest_month) %>% 
  scale() #center and scale the data 

cor.norm_all = cor(allclim_normalized) #test correlations among the traits
cor.sig_all <- cor.mtest(allclim_normalized, method = "pearson") #get pearson's test p-values 
cor.norm_all
##                              cwd         pck          ppt          tmn
## cwd                  1.000000000 -0.23024102 -0.525011959  0.048818582
## pck                 -0.230241025  1.00000000  0.592726313 -0.617432507
## ppt                 -0.525011959  0.59272631  1.000000000 -0.217874536
## tmn                  0.048818582 -0.61743251 -0.217874536  1.000000000
## tmx                  0.129392756 -0.65141841 -0.314879796  0.945915908
## ann_tmean            0.091503625 -0.64366737 -0.271458128  0.985590281
## mean_diurnal_range   0.256742803 -0.21069291 -0.336395878  0.004857161
## temp_seasonality     0.082109914  0.09904656 -0.017533688  0.094406150
## temp_ann_range       0.005302811  0.09945198 -0.075099238  0.106095970
## ann_ppt             -0.531190611  0.67538376  0.846803235 -0.188347541
## ppt_seasonality      0.307883007 -0.08861305 -0.113889591  0.213038297
## tmean_wettest_month -0.066033372 -0.51844943 -0.161838695  0.728562618
## tmean_driest_month  -0.116950951 -0.33647875  0.009749743  0.752422219
## ppt_warmest_month   -0.061396646  0.14949739  0.137928377 -0.346946482
## ppt_coldest_month   -0.209314331  0.39511590  0.628038152 -0.098902143
##                             tmx   ann_tmean mean_diurnal_range temp_seasonality
## cwd                  0.12939276  0.09150362        0.256742803       0.08210991
## pck                 -0.65141841 -0.64366737       -0.210692906       0.09904656
## ppt                 -0.31487980 -0.27145813       -0.336395878      -0.01753369
## tmn                  0.94591591  0.98559028        0.004857161       0.09440615
## tmx                  1.00000000  0.98715987        0.329002561       0.07394074
## ann_tmean            0.98715987  1.00000000        0.173935398       0.08503723
## mean_diurnal_range   0.32900256  0.17393540        1.000000000      -0.04688670
## temp_seasonality     0.07394074  0.08503723       -0.046886699       1.00000000
## temp_ann_range       0.19623415  0.15455760        0.296050556       0.71934975
## ann_ppt             -0.28813168 -0.24297288       -0.339895732       0.18479465
## ppt_seasonality      0.21531260  0.21716194        0.043561320       0.15269917
## tmean_wettest_month  0.74190024  0.74556383        0.166111828      -0.21033028
## tmean_driest_month   0.73139529  0.75183458        0.064277891       0.25912011
## ppt_warmest_month   -0.33996671 -0.34809160       -0.038010528      -0.24471956
## ppt_coldest_month   -0.18081016 -0.14297332       -0.269447144       0.09560980
##                     temp_ann_range     ann_ppt ppt_seasonality
## cwd                    0.005302811 -0.53119061     0.307883007
## pck                    0.099451976  0.67538376    -0.088613051
## ppt                   -0.075099238  0.84680324    -0.113889591
## tmn                    0.106095970 -0.18834754     0.213038297
## tmx                    0.196234149 -0.28813168     0.215312599
## ann_tmean              0.154557597 -0.24297288     0.217161936
## mean_diurnal_range     0.296050556 -0.33989573     0.043561320
## temp_seasonality       0.719349749  0.18479465     0.152699169
## temp_ann_range         1.000000000  0.25141595    -0.009507449
## ann_ppt                0.251415953  1.00000000    -0.145678854
## ppt_seasonality       -0.009507449 -0.14567885     1.000000000
## tmean_wettest_month   -0.015028498 -0.15957747    -0.086884413
## tmean_driest_month     0.367053984  0.11239644     0.044144125
## ppt_warmest_month     -0.349911616 -0.04406956    -0.148612140
## ppt_coldest_month     -0.093299711  0.52573340     0.322191092
##                     tmean_wettest_month tmean_driest_month ppt_warmest_month
## cwd                         -0.06603337       -0.116950951       -0.06139665
## pck                         -0.51844943       -0.336478752        0.14949739
## ppt                         -0.16183869        0.009749743        0.13792838
## tmn                          0.72856262        0.752422219       -0.34694648
## tmx                          0.74190024        0.731395285       -0.33996671
## ann_tmean                    0.74556383        0.751834584       -0.34809160
## mean_diurnal_range           0.16611183        0.064277891       -0.03801053
## temp_seasonality            -0.21033028        0.259120110       -0.24471956
## temp_ann_range              -0.01502850        0.367053984       -0.34991162
## ann_ppt                     -0.15957747        0.112396440       -0.04406956
## ppt_seasonality             -0.08688441        0.044144125       -0.14861214
## tmean_wettest_month          1.00000000        0.557261085       -0.23940794
## tmean_driest_month           0.55726109        1.000000000       -0.45168909
## ppt_warmest_month           -0.23940794       -0.451689094        1.00000000
## ppt_coldest_month           -0.33077102       -0.006376116        0.03678450
##                     ppt_coldest_month
## cwd                      -0.209314331
## pck                       0.395115902
## ppt                       0.628038152
## tmn                      -0.098902143
## tmx                      -0.180810164
## ann_tmean                -0.142973316
## mean_diurnal_range       -0.269447144
## temp_seasonality          0.095609798
## temp_ann_range           -0.093299711
## ann_ppt                   0.525733400
## ppt_seasonality           0.322191092
## tmean_wettest_month      -0.330771021
## tmean_driest_month       -0.006376116
## ppt_warmest_month         0.036784497
## ppt_coldest_month         1.000000000
cor.sig_all$p
##                               cwd           pck           ppt           tmn
## cwd                  0.000000e+00  5.397865e-36 5.224883e-204  8.749996e-03
## pck                  5.397865e-36  0.000000e+00 4.213310e-273 1.219998e-302
## ppt                 5.224883e-204 4.213310e-273  0.000000e+00  2.547516e-32
## tmn                  8.749996e-03 1.219998e-302  2.547516e-32  0.000000e+00
## tmx                  3.083139e-12  0.000000e+00  2.238851e-67  0.000000e+00
## ann_tmean            8.594787e-07  0.000000e+00  7.069807e-50  0.000000e+00
## mean_diurnal_range   1.261478e-44  2.745629e-30  3.263482e-77  7.943341e-01
## temp_seasonality     1.013288e-05  9.877402e-08  3.466483e-01  3.811275e-07
## temp_ann_range       7.759464e-01  8.752458e-08  5.430112e-05  1.127610e-08
## ann_ppt             1.125419e-209  0.000000e+00  0.000000e+00  1.980981e-24
## ppt_seasonality      2.390022e-64  1.886061e-06  8.653620e-10  6.066715e-31
## tmean_wettest_month  3.883114e-04 4.072443e-198  2.257424e-18  0.000000e+00
## tmean_driest_month   3.007039e-10  2.980136e-77  6.007766e-01  0.000000e+00
## ppt_warmest_month    9.727645e-04  7.090458e-16  1.020417e-13  2.475412e-82
## ppt_coldest_month    6.612899e-30 2.423673e-108 3.799079e-316  1.031095e-07
##                              tmx    ann_tmean mean_diurnal_range
## cwd                 3.083139e-12 8.594787e-07       1.261478e-44
## pck                 0.000000e+00 0.000000e+00       2.745629e-30
## ppt                 2.238851e-67 7.069807e-50       3.263482e-77
## tmn                 0.000000e+00 0.000000e+00       7.943341e-01
## tmx                 0.000000e+00 0.000000e+00       9.639228e-74
## ann_tmean           0.000000e+00 0.000000e+00       5.107329e-21
## mean_diurnal_range  9.639228e-74 5.107329e-21       0.000000e+00
## temp_seasonality    7.071682e-05 4.824999e-06       1.180879e-02
## temp_ann_range      2.037142e-26 7.100763e-17       2.063513e-59
## ann_ppt             3.092916e-56 5.202871e-40       6.875194e-79
## ppt_seasonality     1.379263e-31 4.084059e-32       1.933238e-02
## tmean_wettest_month 0.000000e+00 0.000000e+00       2.764644e-19
## tmean_driest_month  0.000000e+00 0.000000e+00       5.535993e-04
## ppt_warmest_month   6.354197e-79 6.697575e-83       4.127334e-02
## ppt_coldest_month   1.307437e-22 1.228223e-14       3.861977e-49
##                     temp_seasonality temp_ann_range       ann_ppt
## cwd                     1.013288e-05   7.759464e-01 1.125419e-209
## pck                     9.877402e-08   8.752458e-08  0.000000e+00
## ppt                     3.466483e-01   5.430112e-05  0.000000e+00
## tmn                     3.811275e-07   1.127610e-08  1.980981e-24
## tmx                     7.071682e-05   2.037142e-26  3.092916e-56
## ann_tmean               4.824999e-06   7.100763e-17  5.202871e-40
## mean_diurnal_range      1.180879e-02   2.063513e-59  6.875194e-79
## temp_seasonality        0.000000e+00   0.000000e+00  1.459782e-23
## temp_ann_range          0.000000e+00   0.000000e+00  8.321491e-43
## ann_ppt                 1.459782e-23   8.321491e-43  0.000000e+00
## ppt_seasonality         1.668311e-16   6.098560e-01  3.823169e-15
## tmean_wettest_month     3.461959e-30   4.198800e-01  6.703594e-18
## tmean_driest_month      1.883741e-45   1.186802e-92  1.434737e-09
## ppt_warmest_month       1.401398e-40   8.292478e-84  1.796319e-02
## ppt_coldest_month       2.701105e-07   5.211126e-07 1.154627e-204
##                     ppt_seasonality tmean_wettest_month tmean_driest_month
## cwd                    2.390022e-64        3.883114e-04       3.007039e-10
## pck                    1.886061e-06       4.072443e-198       2.980136e-77
## ppt                    8.653620e-10        2.257424e-18       6.007766e-01
## tmn                    6.066715e-31        0.000000e+00       0.000000e+00
## tmx                    1.379263e-31        0.000000e+00       0.000000e+00
## ann_tmean              4.084059e-32        0.000000e+00       0.000000e+00
## mean_diurnal_range     1.933238e-02        2.764644e-19       5.535993e-04
## temp_seasonality       1.668311e-16        3.461959e-30       1.883741e-45
## temp_ann_range         6.098560e-01        4.198800e-01       1.186802e-92
## ann_ppt                3.823169e-15        6.703594e-18       1.434737e-09
## ppt_seasonality        0.000000e+00        2.983559e-06       1.776964e-02
## tmean_wettest_month    2.983559e-06        0.000000e+00      6.282549e-235
## tmean_driest_month     1.776964e-02       6.282549e-235       0.000000e+00
## ppt_warmest_month      1.052027e-15        7.325911e-39      5.805950e-145
## ppt_coldest_month      1.250506e-70        1.454288e-74       7.321918e-01
##                     ppt_warmest_month ppt_coldest_month
## cwd                      9.727645e-04      6.612899e-30
## pck                      7.090458e-16     2.423673e-108
## ppt                      1.020417e-13     3.799079e-316
## tmn                      2.475412e-82      1.031095e-07
## tmx                      6.354197e-79      1.307437e-22
## ann_tmean                6.697575e-83      1.228223e-14
## mean_diurnal_range       4.127334e-02      3.861977e-49
## temp_seasonality         1.401398e-40      2.701105e-07
## temp_ann_range           8.292478e-84      5.211126e-07
## ann_ppt                  1.796319e-02     1.154627e-204
## ppt_seasonality          1.052027e-15      1.250506e-70
## tmean_wettest_month      7.325911e-39      1.454288e-74
## tmean_driest_month      5.805950e-145      7.321918e-01
## ppt_warmest_month        0.000000e+00      4.827916e-02
## ppt_coldest_month        4.827916e-02      0.000000e+00
#tmn and tmx highly correlated with ann_tmean (~98%)   - only keep ann_tmean 
#JRG: keep all for comparisons between water year, etc? 

allclim.pc = prcomp(all_clim[c(8:10, 13:22)], scale = TRUE, center = TRUE) #removed tmn and tmx 
summary(allclim.pc)
## Importance of components:
##                           PC1    PC2    PC3    PC4     PC5     PC6     PC7
## Standard deviation     1.9662 1.6168 1.3715 1.1882 0.93517 0.83517 0.70695
## Proportion of Variance 0.2974 0.2011 0.1447 0.1086 0.06727 0.05365 0.03844
## Cumulative Proportion  0.2974 0.4985 0.6432 0.7518 0.81903 0.87268 0.91113
##                            PC8     PC9    PC10    PC11    PC12   PC13
## Standard deviation     0.63094 0.49809 0.42335 0.40931 0.31821 0.2473
## Proportion of Variance 0.03062 0.01908 0.01379 0.01289 0.00779 0.0047
## Cumulative Proportion  0.94175 0.96083 0.97462 0.98751 0.99530 1.0000
tibble(PC=str_c("PC",str_pad(1:13,2,pad="0")),
       percent_var=allclim.pc$sdev[1:13]^2/sum(allclim.pc$sdev^2)*100) %>%
  ggplot(aes(x=PC, y=percent_var)) +
  geom_col() +
  ggtitle("Percent Variance Explained")

#combine pcs with metadata
allclim.pc.dat = data.frame(allclim.pc$x)
allclim_locs.pc = cbind(all_clim, allclim.pc.dat)
allclim_loadings = data.frame(varnames=rownames(allclim.pc$rotation), allclim.pc$rotation)
allclim_loadings
##                                varnames          PC1         PC2          PC3
## cwd                                 cwd -0.226641423 -0.21641835 -0.370029608
## pck                                 pck  0.423649215  0.04393611 -0.159554618
## ppt                                 ppt  0.398899739  0.23355922  0.222430506
## ann_tmean                     ann_tmean -0.370447663  0.30244521  0.204643533
## mean_diurnal_range   mean_diurnal_range -0.227299890 -0.05238903 -0.191302363
## temp_seasonality       temp_seasonality  0.002261185  0.33580034 -0.492170802
## temp_ann_range           temp_ann_range -0.062622334  0.38425529 -0.431571349
## ann_ppt                         ann_ppt  0.378470452  0.35455520  0.083032217
## ppt_seasonality         ppt_seasonality -0.071944277  0.04689868 -0.249367315
## tmean_wettest_month tmean_wettest_month -0.314565536  0.19758349  0.418996813
## tmean_driest_month   tmean_driest_month -0.223322890  0.47617628  0.153064421
## ppt_warmest_month     ppt_warmest_month  0.164905489 -0.33832530  0.114500766
## ppt_coldest_month     ppt_coldest_month  0.304592109  0.18410630 -0.002776084
##                             PC4          PC5         PC6         PC7
## cwd                 -0.23350031  0.007173260  0.12539528  0.79711924
## pck                  0.10207661  0.140491414  0.24265974  0.28032528
## ppt                 -0.06988683  0.197299117 -0.01798083  0.18423592
## ann_tmean           -0.19095043  0.058966748 -0.18864269  0.10129942
## mean_diurnal_range   0.20749140  0.842590559  0.11101808 -0.15023870
## temp_seasonality     0.07540442 -0.266170231 -0.39217534 -0.01193811
## temp_ann_range       0.33237398  0.109666108 -0.10102675 -0.04367603
## ann_ppt              0.07714491  0.092310003  0.07646234  0.18793444
## ppt_seasonality     -0.68906711  0.158171094 -0.01849579 -0.24573960
## tmean_wettest_month  0.05456145  0.120995184  0.02559051  0.24739161
## tmean_driest_month  -0.03335211  0.006059012 -0.09358084  0.14724983
## ppt_warmest_month    0.06451669  0.238795985 -0.83169406  0.18559759
## ppt_coldest_month   -0.49546364  0.199337127 -0.07638160 -0.06444794
##                             PC8          PC9        PC10        PC11
## cwd                 -0.15418410  0.070137147 -0.12513083 -0.11681292
## pck                  0.35128112 -0.333498490  0.47819756  0.34249867
## ppt                 -0.05338805  0.210791531 -0.56708594  0.17977979
## ann_tmean            0.01514374 -0.000608545  0.16718075 -0.01168467
## mean_diurnal_range  -0.15758687  0.031634258 -0.09718303  0.22844391
## temp_seasonality    -0.02887030  0.280852630 -0.03538072  0.54579311
## temp_ann_range       0.11307145  0.059714641  0.11194971 -0.57286356
## ann_ppt              0.16527053  0.083101874 -0.16855370 -0.33074403
## ppt_seasonality      0.56063842 -0.091081138 -0.18172951 -0.04017070
## tmean_wettest_month  0.36007434  0.452269536  0.27960945  0.12516156
## tmean_driest_month  -0.19686392 -0.694879950 -0.12382360  0.10141762
## ppt_warmest_month    0.14049232 -0.141607570  0.05260857 -0.10381613
## ppt_coldest_month   -0.53058471  0.182540562  0.47453879 -0.10299838
##                            PC12         PC13
## cwd                  0.02701190 -0.021292334
## pck                 -0.14017896  0.158939435
## ppt                 -0.04792819  0.505261078
## ann_tmean           -0.77933097  0.089203511
## mean_diurnal_range  -0.04374820 -0.178061594
## temp_seasonality     0.01668055 -0.188129326
## temp_ann_range       0.09203078  0.402533626
## ann_ppt             -0.13373845 -0.691816343
## ppt_seasonality      0.11226393 -0.005014547
## tmean_wettest_month  0.42299303  0.002337460
## tmean_driest_month   0.34465316 -0.048458164
## ppt_warmest_month    0.06031327 -0.030173056
## ppt_coldest_month    0.16573708  0.002811904
#PC1 pos snowpack, pos ppt and ann_ppt, and negative ann_tmean
#PC2 pos tmean_driest_month, pos temp_ann_range, pos ann_ppt 

Plot PCA with all climate

##plot everything, color by elevation
autoplot(allclim.pc, data = all_clim,
         x=1, y=2,
         colour='elev_m', alpha=0.5,
         label=FALSE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic()

ggsave("../Figures/AllClim_YrlyVar.png", width = 7.4, height = 6, units = "in")

#facet by seasonal summmary 
autoplot(allclim.pc, data = all_clim,
         x=1, y=2,
         colour='elev_m', alpha=0.5,
         label=FALSE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic() +
  facet_wrap(~Season)

#facet by timeperiod 
autoplot(allclim.pc, data = all_clim,
         x=1, y=2,
         colour='elev_m', alpha=0.5,
         label=FALSE, label.label="parent.pop",
         loadings=TRUE, loadings.colour='black', loadings.linewidth = 0.7,
         loadings.label = TRUE, loadings.label.size=6, loadings.label.colour="black", 
         loadings.label.vjust = -0.2, loadings.label.repel=TRUE) +
   scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic() +
  facet_wrap(~timeframe)
## Warning: ggrepel: 1 unlabeled data points (too many overlaps). Consider increasing max.overlaps
## ggrepel: 1 unlabeled data points (too many overlaps). Consider increasing max.overlaps
## ggrepel: 1 unlabeled data points (too many overlaps). Consider increasing max.overlaps
## ggrepel: 1 unlabeled data points (too many overlaps). Consider increasing max.overlaps

#this one isn't pretty because of the garden 

Hulls prep

allclim_locs.pc_nogarden = allclim_locs.pc %>%
                          filter(parent.pop != "WL2_Garden")
allclim_locs.pc_garden = allclim_locs.pc %>%
  filter(parent.pop == "WL2_Garden", year>2022) %>% 
  mutate(year=as.character(year))

#plot with no vectors 
pca_all_novect12 =  ggplot(allclim_locs.pc_nogarden, aes(x = PC1, y = PC2, colour= elev_m)) +
  geom_point(aes(colour = elev_m), size = 3, alpha=0.5) +
  scale_colour_gradient(low = "#F5A540", high = "#0043F0") +
  labs(x="PC1 (29.74%)", y="PC2 (20.11%)", color="Elevation (m)") +
  geom_vline(xintercept = 0, linetype="dashed") + geom_hline(yintercept = 0, linetype="dashed") +
  theme_classic() +
  theme(text=element_text(size=28))
pca_all_novect12

Hulls by recent vs. historic

pca_hull_recentvhistoric_12 <- 
  allclim_locs.pc_nogarden %>% 
  group_by(timeframe, Season) %>% 
  slice(chull(PC1, PC2))

pca_chull_recvhist = pca_all_novect12 +
  geom_polygon(data = pca_hull_recentvhistoric_12,
               aes(x = PC1, y = PC2, group = timeframe, linetype = timeframe, fill = timeframe), 
               color = "black", alpha = 0.3, show.legend = T) + 
  scale_fill_manual(values = c("gray72", "gray22")) +
  facet_wrap(~Season)
pca_chull_recvhist

#add back in WL2 garden values
pca_chull_recvhist_wgarden = pca_chull_recvhist + 
  geom_point(data = allclim_locs.pc_garden, aes(shape=year),
              size=6, color = "purple")  +
     scale_shape_discrete(name = "WL2 Garden") 
pca_chull_recvhist_wgarden

ggsave("../Figures/AllClim_YrlyVar_TimePdHulls.png", width = 14.8, height = 6, units = "in")

Hulls by water year vs growth season

pca_hull_season_12 <- 
  allclim_locs.pc_nogarden %>% 
  group_by(Season, timeframe) %>% 
  slice(chull(PC1, PC2))

pca_chull_season = pca_all_novect12 +
  geom_polygon(data = pca_hull_season_12,
               aes(x = PC1, y = PC2, group = Season, linetype = Season, fill = Season), 
               color = "black", alpha = 0.3, show.legend = T) + 
  scale_fill_manual(values = c("gray72", "gray22")) +
  facet_wrap(~timeframe)
pca_chull_season

#add back in WL2 garden values
allclim_locs.pc_garden_2 <- allclim_locs.pc_garden %>% 
  mutate(timeframe="Recent")
allclim_locs.pc_garden_3 <- allclim_locs.pc_garden %>% 
  mutate(timeframe="Historic")
allclim_locs.pc_garden_4 <- bind_rows(allclim_locs.pc_garden_2, allclim_locs.pc_garden_3) %>% 
  mutate(seasontime = str_remove(seasontime, "Garden"))

pca_chull_season_wgarden = pca_chull_season + 
  geom_point(data = allclim_locs.pc_garden_4, aes(shape=seasontime),
              size=6, color = "purple")  +
  scale_shape_manual(values=c(15, 16, 17, 18), name = "WL2 Garden") 
pca_chull_season_wgarden

ggsave("../Figures/AllClim_YrlyVar_SeasonHulls.png", width = 14.8, height = 6, units = "in")